如何从Java中的XML节点提取相同的多个属性 [英] How can I extract the multiple attributes that are the same, from an XML node in Java

查看:167
本文介绍了如何从Java中的XML节点提取相同的多个属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个Garmin Sat Nav的XML文件,我想从Java中提取数据。但是,我找不到一种提取TRACK纬度和经度的方法,因为在单个节点中有多个具有相同名称的属性。我曾想过使用 getAttribute( lat)之类的东西,但遗憾的是,这只会返回第一个音轨段的第一个纬度:-(

I currently have an XML file from a Garmin Sat Nav, which I want to extract data from using Java. However, I cannot find a way to extract the TRACK latitudes and longitudes, because there are multiple attributes with the same name, within a single node. I thought of using something like "getAttribute("lat")", but apparntly this would only return the first latitude of the first track segment :-(

我还没有'尚未启动该项目,因为从XML文件提取纬度和经度至关重要。

I haven't yet started the project, because it is crucial that I extract the latitudes and longitudes from the XML file.

任何帮助将不胜感激:-)

Any help would be greatly appreciated :-)

XML文件

<gpx xmlns="http://www.topografix.com/GPX/1/1"
xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3"
xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" creator="Oregon
400t" version="1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.topografix.com/GPX/1/1
http://www.topografix.com/GPX/1/1/gpx.xsd 
http://www.garmin.com/xmlschemas/GpxExtensions/v3
http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd
http://www.garmin.com/xmlschemas/TrackPointExtension/v1
http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd">
<metadata>
  <link href="http://www.garmin.com">
    <text>Garmin International</text>
  </link>
    <time>2009-10-17T22:58:43Z</time>
</metadata>
<trk>
<name>Example GPX Document</name>
<trkseg>
  <trkpt lat="47.644548" lon="-122.326897">
    <ele>4.46</ele>
    <time>2009-10-17T18:37:26Z</time>
  </trkpt>
  <trkpt lat="47.644548" lon="-122.326897">
    <ele>4.94</ele>
    <time>2009-10-17T18:37:31Z</time>
  </trkpt>
  <trkpt lat="47.644548" lon="-122.326897">
    <ele>6.87</ele>
    <time>2009-10-17T18:37:34Z</time>
  </trkpt>
</trkseg>


您好,我尝试了下面的代码,但是由于某种原因,我正在检索一个值,我什至无法在我的XML文件中找到它!!

Hi, I tried the below code, but for some reason I'm retrieving a value, that I can't even find in my XML file!!!

package Test;

import java.io.File;
import javax.xml.parsers.*;
import javax.xml.xpath.*;
import org.w3c.dom.Document;

public class ReadXMLFile2 {

    public static void main(String[] args) throws Exception {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
    Document document = db.parse(new File("J:/Desktop/Current.gpx"));

    XPath xpath = XPathFactory.newInstance().newXPath();
    System.out.println(xpath.evaluate("//trkseg/trkpt/@lat", document));
    } 

}

推荐答案

为了能够使用XPath,首先需要使您的 DocumentBuilderFactory 名称空间可识别(令人讨厌的是,默认情况下,大概是出于历史原因),因为XPath语言需要名称空间支持。

In order to be able to use XPath you first need to make your DocumentBuilderFactory namespace-aware (annoyingly it isn't by default, presumably for historical reasons) since the XPath language requires namespace support.

现在 xmlns = http://www.topografix.com/GPX XML文件顶部的/ 1/1 表示文档中所有未加前缀的元素名称都属于该名称空间。为了将它们与XPath匹配,因此,您需要定义 NamespaceContext 将此名称空间URI绑定到前缀,并在XPath表达式中始终使用该前缀。

Now the xmlns="http://www.topografix.com/GPX/1/1" at the top of your XML file means that all the unprefixed element names in the document belong to that namespace. In order to match them with XPath you will therefore need to define a NamespaceContext that binds this namespace URI to a prefix and use that prefix consistently in the XPath expressions.

或者,忘记XPath并使用标准DOM API可能更简单:

Alternatively it may be simpler to forget XPath and just use the standard DOM APIs:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// enable namespace processing
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(new File("J:/Desktop/Current.gpx"));

NodeList trkpts = document.getElementsByTagNameNS(
           "http://www.topografix.com/GPX/1/1", "trkpt");
for(int i = 0; i < trkpts.getLength(); i++) {
  Element pt = (Element)trkpts.item(i);
  // since namespaces are enabled we must use the DOM level 2 getAttributeNS,
  // not the legacy getAttribute, even though the attributes we're getting
  // do not themselves belong to a namespace.
  System.out.println("lat: " + pt.getAttributeNS(null, "lat") +
      ", long: " + pt.getAttributeNS(null, "lon"));
}

这篇关于如何从Java中的XML节点提取相同的多个属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆