【问题标题】:Parsing XML Node in Java using XPATH使用 XPATH 在 Java 中解析 XML 节点
【发布时间】:2011-11-09 06:04:37
【问题描述】:

我刚开始尝试 Jaxp13XPathTemplate,但对解析 XML 有点困惑。

这里是示例 XML

<fxDataSets> 
<fxDataSet name="NAME_A">
  <link rel="self" href="http://localhost:8080/linkA"/>
  <baseCurrency>EUR</baseCurrency>
  <description>TEST DESCRIPTION A</description>
</fxDataSet>

<fxDataSet name="NAME_B">
  <link rel="self" href="http://localhost:8080/linkB"/>
  <baseCurrency>EUR</baseCurrency>
  <description>TEST DESCRIPTION B</description>
</fxDataSet>
<fxDataSets>  

我已经能够获取 NAME_A 和 NAME_B,但是我无法获取这两个节点的描述。

这是我想出的。

XPathOperations  xpathTemplate = new Jaxp13XPathTemplate();
    String fxRateURL = "http://localhost:8080/rate/datasets";
    RestTemplate restTemplate = new RestTemplate();
    Source fxRate = restTemplate.getForObject(fxRateURL,Source.class);
    List<Map<String, Object>> currencyList = xpathTemplate.evaluate("//fxDataSet", fxRate , new NodeMapper() {
        public Object mapNode(Node node, int i) throws DOMException 
        {
            Map<String, Object> singleFXMap = new HashMap<String, Object>();
            Element fxDataSet = (Element) node;
            String id    = fxDataSet.getAttribute("name");

            /* This part is not working
            if(fxDataSet.hasChildNodes())
            {
                NodeList nodeList = fxDataSet.getChildNodes();
                int length = nodeList.getLength();

                for(int index=0;i<length;i++)
                {
                    Node childNode = nodeList.item(index);
                    System.out.println("childNode name"+childNode.getLocalName()+":"+childNode.getNodeValue());
                }

            }*/

            return new Object();
        }
    });

【问题讨论】:

    标签: java xpath xml-parsing


    【解决方案1】:

    尝试使用 dom4j 库,它是 saxReader。

        InputStream is = FileUtils.class.getResourceAsStream("file.xml");
    
        SAXReader reader = new SAXReader();
        org.dom4j.Document doc = reader.read(is);
        is.close();
        Element content = doc.getRootElement();  //this will return the root element in your xml file
        List<Element> methodEls = content.elements("element"); // this will retun List of all Elements with name "element" 
    

    【讨论】:

    • 嗨 Ademiban,我已经拿到了 Element。这里的问题是如何获取描述标签值,而不是获取元素。
    【解决方案2】:

    看看public <T> List<T> evaluate(String expression, Source context, NodeMapper<T> nodeMapper)

    • evaluateNodeMapper&lt;T&gt; 作为其参数之一
    • 它返回List&lt;T&gt;类型的对象

    但是对于你给定的代码 sn-p:

    • new NodeMapper() 作为参数传递
    • 但试图返回 List&lt;Map&lt;String, Object&gt;&gt; 这肯定违反了 api 的合同。

    可能的解决方案:

    我假设你想返回一个 FxDataSet 类型的对象,它包装了 &lt;fxDataSet&gt;...&lt;/fxDataSet&gt; 元素。如果是这样的话,

    • 将参数作为new NodeMapper&lt;FxDataSet&gt;()作为参数传递
    • 使用List&lt;FxDataSet&gt; currencyList = ...作为左侧表达式;
    • 将方法返回类型更改为public FxDataSet mapNode(Node node, int i) throws DOMException

    查看NodeMapper 的文档。

    当然,我没有使用过Jaxp13XPathTemplate,但这应该是常见的 Java 概念,它帮助我找出问题所在。我希望这个解决方案能够奏效。

    【讨论】:

      【解决方案3】:

      如果你想获取 fxDataSet 元素的子节点,你应该可以这样做:

      Node descriptionNode= fxDataSet.getElementsByTagName("description").item(0);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-11-25
        • 1970-01-01
        • 1970-01-01
        • 2013-02-20
        • 1970-01-01
        • 2014-03-06
        • 2010-09-25
        • 1970-01-01
        相关资源
        最近更新 更多