Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用属性旁边的数据在Java中解析XML_Java_Xml_Parsing_Openstreetmap - Fatal编程技术网

使用属性旁边的数据在Java中解析XML

使用属性旁边的数据在Java中解析XML,java,xml,parsing,openstreetmap,Java,Xml,Parsing,Openstreetmap,我需要获取Java中某些XML对象的值,但它们位于属性标记中。我不知道该怎么办 XML示例: <node id="359832" version="5" timestamp="2008-05-20T15:20:46Z" uid="4499" changeset="486842" lat="50.9051565" lon="6.963755"> <tag k="amenity" v="restaurant"/> <tag k="name" v="Cam

我需要获取Java中某些XML对象的值,但它们位于属性标记中。我不知道该怎么办

XML示例:

<node id="359832" version="5" timestamp="2008-05-20T15:20:46Z" uid="4499" changeset="486842" lat="50.9051565" lon="6.963755">
    <tag k="amenity" v="restaurant"/>
    <tag k="name" v="Campus"/>
  </node>
  <node id="451153" version="4" timestamp="2009-09-17T18:09:14Z" uid="508" changeset="2514480" lat="51.6020306" lon="-0.1935029">
    <tag k="amenity" v="restaurant"/>
    <tag k="created_by" v="JOSM"/>
    <tag k="name" v="Sun and Sea"/>
  </node>

我已经查看了,但是找不到关于如何获取
lat
lon
的值的任何信息,因为它们位于属性旁边,而不是嵌套的。我不需要使用输入流,xml文件太小了,我无法将其存储在内存中。

您使用的是什么解析库?我正在尝试使用DOM,但我似乎不知道如何做到这一点。您使用的是什么解析库?我正在尝试使用DOM,但似乎不知道如何做到这一点。
package com.sandbox;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;

public class Sandbox {

    public static void main(String argv[]) throws IOException, SAXException, ParserConfigurationException {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.parse(Sandbox.class.getResourceAsStream("/foo.xml"));

        NodeList nodeNodeList = document.getElementsByTagName("node");

        for (int i = 0; i < nodeNodeList.getLength(); i++) {

            Node nNode = nodeNodeList.item(i);

            System.out.println(nNode.getAttributes().getNamedItem("lat").getNodeValue());
            System.out.println(nNode.getAttributes().getNamedItem("lon").getNodeValue());

        }

    }


}
package com.sandbox;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;

public class Sandbox {

    public static void main(String argv[]) throws IOException, SAXException, ParserConfigurationException {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.parse(Sandbox.class.getResourceAsStream("/foo.xml"));

        NodeList nodeNodeList = document.getElementsByTagName("node");

        for (int i = 0; i < nodeNodeList.getLength(); i++) {

            Node nNode = nodeNodeList.item(i);

            System.out.println(nNode.getAttributes().getNamedItem("lat").getNodeValue());
            System.out.println(nNode.getAttributes().getNamedItem("lon").getNodeValue());

        }

    }


}
50.9051565
6.963755
51.6020306
-0.1935029