使用Java解析XML [英] Using Java to parse XML

查看:853
本文介绍了使用Java解析XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个PHP脚本来解析XML文件.这不容易使用,我想用Java实现它.

I have made a PHP script that parses an XML file. This is not easy to use and I wanted to implement it in Java.

在第一个元素内,我循环遍历各种wfs:member个元素:

Inside the first element there are various count of wfs:member elements I loop through:

foreach ($data->children("wfs", true)->member as $member) { }

使用Java很容易做到这一点:

This was easy to do with Java:

NodeList wfsMember = doc.getElementsByTagName("wfs:member");
for(int i = 0; i < wfsMember.getLength(); i++) { }

我已经打开了这样的XML文件

I have opened the XML file like this

DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document doc = documentBuilder.parse(WeatherDatabaseUpdater.class.getResourceAsStream("wfs.xml"));

然后,我需要从名为observerdProperty的元素获取属性.在PHP中,这很简单:

Then I need to get a attribute from an element called observerdProperty. In PHP this is simple:

$member->
    children("omso", true)->PointTimeSeriesObservation->
    children("om", true)->observedProperty->
    attributes("xlink", true)->href

但是在Java中,我该怎么做?

But in Java, how do I do this? Do I need to use getElementsByTagName and loop through them if I want to go deeper in the structure?`

在PHP中,整个脚本如下所示.

In PHP the whole script looks the following.

foreach ($data->children("wfs", true)->member as $member) {
    $dataType = $dataTypes[(string) $member->
                    children("omso", true)->PointTimeSeriesObservation->
                    children("om", true)->observedProperty->
                    attributes("xlink", true)->href];

    foreach ($member->
            children("omso", true)->PointTimeSeriesObservation->
            children("om", true)->result->
            children("wml2", true)->MeasurementTimeseries->
            children("wml2", true)->point as $point) {

        $time = $point->children("wml2", true)->MeasurementTVP->children("wml2", true)->time;
        $value = $point->children("wml2", true)->MeasurementTVP->children("wml2", true)->value;

        $data[$dataType][] = array($time, $value)
    }
}

在第二个foreach中,我遍历观察元素并从中获取时间和值数据.然后将其保存在数组中.如果需要按照我描述的方式遍历Java中的元素,则很难实现.我认为情况并非如此,所以有人可以建议我如何在Java中实现类似的东西吗?

In the second foreach I loop through the observation elements and get the time and value data from it. Then I save it in an array. If I need to loop through the elements in Java the way I described, this is very hard to implement. I don't think that is the case, so could someone advice me how to implement something similar in Java?

推荐答案

如果性能不是主要问题,最简单的方法可能就是XPath.使用XPath,您只需指定路径即可查找节点和属性.

The easiest way, if performance is not a main concern, is probably XPath. With XPath, you can find nodes and attributes simply by specifying a path.

XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile(<xpath_expression>);
NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);

xpath_expression可能很简单

The xpath_expression could be as simple as

"string(//member/observedProperty/@href)"

有关XPath的更多信息, W3Schools的XPath教程很好.

For more information about XPath, XPath Tutorial from W3Schools is pretty good.

这篇关于使用Java解析XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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