Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.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_Openstreetmap - Fatal编程技术网

使用java从XML文件获取节点的所有信息

使用java从XML文件获取节点的所有信息,java,xml,openstreetmap,Java,Xml,Openstreetmap,我有一个学校项目来读取osm文件并创建数据库。现在,我需要从文件中提取纬度、经度和所有餐馆的名称。这是我的代码和osm文件的一部分。我需要知道如何提取想要的值。谢谢大家! DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(map);

我有一个学校项目来读取osm文件并创建数据库。现在,我需要从文件中提取纬度、经度和所有餐馆的名称。这是我的代码和osm文件的一部分。我需要知道如何提取想要的值。谢谢大家!

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(map);
        
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile("//node[@lat>0]");
NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
        
for(int i=0; i<nl.getLength(); i++) {
    System.out.println(nl.item(i).getNodeName()); //returns "node"
}
DocumentBuilderFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder=factory.newDocumentBuilder();
文档doc=builder.parse(map);
XPathFactory XPathFactory=XPathFactory.newInstance();
XPath=xPathfactory.newXPath();
XPathExpression expr=xpath.compile(“//节点[@lat>0]”);
NodeList nl=(NodeList)expr.evaluate(doc,XPathConstants.NODESET);
对于(inti=0;i
DocumentBuilderFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder=factory.newDocumentBuilder();
文档doc=builder.parse(map);
XPathFactory XPathFactory=XPathFactory.newInstance();
XPath=xPathfactory.newXPath();
XPathExpression expr=xpath.compile(“//节点[@lat>0]”);
NodeList nl=(NodeList)expr.evaluate(doc,XPathConstants.NODESET);
对于(int i=0;i
<node id="6814246387" visible="true" version="1" changeset="74752372" timestamp="2019-09-21T15:31:14Z" user="YunDi88" uid="9047835" lat="42.0015821" lon="21.4189761">
  <tag k="amenity" v="restaurant"/>
  <tag k="name" v="Restaurant"/>
 </node>
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(map);
        
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile("//node[@lat>0]");
NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
        
for(int i=0; i<nl.getLength(); i++) {
    Node currentItem=nl.item(i);
    String value=currentItem.getAttributes().getNamedItem("lat").getNodeValue();
    System.out.println(value);
}