从XML中提取元素的属性

时间:2013-11-26 05:03:12

标签: java xml xpath

我正在尝试为元素“way”下的每个“nd”提取“ref”信息。我曾尝试使用常规的java xml解析器和XPath,但仍然无法弄清楚如何检索多个子节点。

<?xml version='1.0' encoding='UTF-8'?>
<osm version="0.6" generator="Osmosis 0.43.1">
  <node id="95946773" version="3" timestamp="2009-11-14T15:52:46Z" uid="17427" user="dysteleologist" changeset="3114170" lat="26.2870135" lon="-80.2120936"/>
  <node id="95946776" version="3" timestamp="2010-10-23T15:07:03Z" uid="207745" user="NE2" changeset="6146275" lat="26.2870058" lon="-80.2128489"/>
<way id="226137459" version="1" timestamp="2013-06-17T20:19:27Z" uid="1119200" user="wagn" changeset="16595491">
    <nd ref="2349898722"/>
    <nd ref="2349898723"/>
    <nd ref="2349898724"/>
    <nd ref="2349898725"/>
    <nd ref="2349898726"/>
    <nd ref="2349898730"/>
    <nd ref="2349898727"/>
    <nd ref="2349898728"/>
    <nd ref="2349898729"/>
    <nd ref="2349898722"/>
    <tag k="building" v="house"/>
  </way>
</osm>

我的代码提取“way”元素。

 NodeList wList = doc.getElementsByTagName("way");
for (int temp = 0; temp < wList.getLength(); temp++) {

    Node wNode = wList.item(temp);

    if (wNode.getNodeType() == Node.ELEMENT_NODE) {
        Element wElement = (Element) wNode;

        output_str = "";
        output_str += wElement.getAttribute("id")+ " ";
                      System.out.println(wElement.getElementsByTagName("nd").item(0).getAttribute("ref"));

我将最后一行打印行更改为

System.out.println(wElement.getElementsByTagName("nd").item(0).getNodeName());

它实际上打印出“nd”作为节点名称,但我无法获取节点的属性。

在javascript中,它的工作原理如下,但这种语法在Java中不起作用。

System.out.println(wElement.getElementsByTagName("nd")[0].getAttribute("ref"));

谢谢,

2 个答案:

答案 0 :(得分:0)

以下代码可用于获取ref属性的值并将其存储在arraylist中。

String s="<?xml version='1.0' encoding='UTF-8'?><osm version=\"0.6\" generator=\"Osmosis 0.43.1\">  <node id=\"95946773\" version=\"3\" timestamp=\"2009-11-14T15:52:46Z\" uid=\"17427\" user=\"dysteleologist\" changeset=\"3114170\" lat=\"26.2870135\" lon=\"-80.2120936\"/>  <node id=\"95946776\" version=\"3\" timestamp=\"2010-10-23T15:07:03Z\" uid=\"207745\" user=\"NE2\" changeset=\"6146275\" lat=\"26.2870058\" lon=\"-80.2128489\"/><way id=\"226137459\" version=\"1\" timestamp=\"2013-06-17T20:19:27Z\" uid=\"1119200\" user=\"wagn\" changeset=\"16595491\">    <nd ref=\"2349898722\"/>    <nd ref=\"2349898723\"/>    <nd ref=\"2349898724\"/>    <nd ref=\"2349898725\"/>    <nd ref=\"2349898726\"/>    <nd ref=\"2349898730\"/>    <nd ref=\"2349898727\"/>    <nd ref=\"2349898728\"/>    <nd ref=\"2349898729\"/>    <nd ref=\"2349898722\"/>    <tag k=\"building\" v=\"house\"/>  </way></osm>";


    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    builderFactory.setNamespaceAware(true);
    InputSource is = new InputSource(new StringReader(s));


    DocumentBuilder builder;
    try {
        builderFactory.setIgnoringElementContentWhitespace(true);
        builder = builderFactory.newDocumentBuilder();
        Document xml = builder.parse(is);
        XPathFactory factory = javax.xml.xpath.XPathFactory.newInstance();
        XPath xPath = factory.newXPath();
        XPathExpression expression;

        expression = xPath.compile("/osm/way/nd/@ref");
        NodeList ss = (NodeList) expression.evaluate(xml,XPathConstants.NODESET);
        ArrayList<String> list = new ArrayList<String>();
        for (int i = 0; i < ss.getLength(); i++) {
            list.add(ss.item(i).getTextContent());
        }
        System.out.println(list);
    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (XPathExpressionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }   
}

答案 1 :(得分:0)

  

此语法在Java中不起作用。

System.out.println(wElement.getElementsByTagName("nd")[0].getAttribute("ref"));

你需要改变两件事。首先,Java使用.item(0)而不是[0],其次是因为Java是强类型的,而getAttributeElement而不是Node的方法,所以你需要一个投:

System.out.println(((Element)wElement.getElementsByTagName("nd").item(0))
                    .getAttribute("ref"));