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