使用jaxb在Java对象中存储xml数据

时间:2021-01-18 21:50:20
<?xml version="1.0" encoding="UTF-8"?>
  <filepaths>
    <application_information_ticker>
      <desc>Ticker1</desc>
      <folder_path>../atlas/info/</folder_path>
    </application_information_ticker>
    <document_management_system>
      <desc></desc>
      <folder_path>../atlas/dms/</folder_path>
    </document_management_system>
  </filepaths>

I have a xml file like this. I need to convert this xml file into java object using JAXB. Because of nested tags, I couldn't perform the operation. Please suggest me a solution for this

我有一个像这样的xml文件。我需要使用JAXB将此xml文件转换为java对象。由于嵌套标签,我无法执行该操作。请为我建议一个解决方案

1 个解决方案

#1


1  

  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  DocumentBuilder builder = factory.newDocumentBuilder();
  InputSource is = new InputSource( new StringReader( xmlString) );
  Document doc = builder.parse( is );

  XPathFactory factory = XPathFactory.newInstance();
  XPath xpath = factory.newXPath();
  xpath.setNamespaceContext(new PersonalNamespaceContext());
  XPathExpression expr = xpath.compile("//src_small/text()");

  Object result = expr.evaluate(doc, XPathConstants.NODESET);
  NodeList nodes = (NodeList) result;
  List<String> list = new ArrayList<String>();
  for (int i = 0; i < nodes.getLength(); i++) {
      list.add (nodes.item(i).getNodeValue());
      System.out.println(nodes.item(i).getNodeValue());

#1


1  

  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  DocumentBuilder builder = factory.newDocumentBuilder();
  InputSource is = new InputSource( new StringReader( xmlString) );
  Document doc = builder.parse( is );

  XPathFactory factory = XPathFactory.newInstance();
  XPath xpath = factory.newXPath();
  xpath.setNamespaceContext(new PersonalNamespaceContext());
  XPathExpression expr = xpath.compile("//src_small/text()");

  Object result = expr.evaluate(doc, XPathConstants.NODESET);
  NodeList nodes = (NodeList) result;
  List<String> list = new ArrayList<String>();
  for (int i = 0; i < nodes.getLength(); i++) {
      list.add (nodes.item(i).getNodeValue());
      System.out.println(nodes.item(i).getNodeValue());