简体   繁体   English

使用STAX API进行XML解析

[英]XML Parsing with STAX API

I am trying to parse XML Structure of Open Street Maps using Stax. 我正在尝试使用Stax解析开放式街道地图的XML结构。 In my implementation I use XMLStreamConstants.START_ELEMENT and XMLStreamConstants.END_ELEMENT to recognize Elements. 在我的实现中,我使用XMLStreamConstants.START_ELEMENTXMLStreamConstants.END_ELEMENT来识别元素。

OpenStreetMaps structure has Elements such as tag which describe as well the node , as well as the way . OpenStreetMaps结构具有元素(例如标签) ,这些元素还描述了节点以及方式 Here is an example of the structure: 这是结构示例:

      <node id="2311741639" ... lat="50.7756648" lon="6.0844948">
       <tag k="entrance" v="yes"/>
      </node>
      <way id="4964449" visible="true" ... uid="67862">
       <nd ref="27290865"/>
        ...
       <tag k="highway" v="residential"/>
        ...
      </way>

How can I distinguish between node and way if parser reads Tag-Element? 如果解析器读取Tag-Element,如何区分节点和方式?

You could use an ArrayDeque of your node representations, or even build a temporary DOM-like structure if the depth of your hierarchy is small. 您可以使用节点表示形式的ArrayDeque ,或者如果层次结构的深度较小,甚至可以构建类似于DOM的临时结构。

Here's an example with ArrayDeque ... 这是ArrayDeque的示例...

Assuming this XML file named stuff.xml : 假设此XML文件名为stuff.xml

<?xml version="1.0" encoding="UTF-8"?>

<stuff>

<node id="2311741639" lat="50.7756648" lon="6.0844948">
    <tag k="entrance" v="yes"/>
</node>

<way id="4964449" visible="true" uid="67862">
    <nd ref="27290865"/>
    <tag k="highway" v="residential"/>
</way>

</stuff>

Assuming the file is on path: /my/path/ 假设文件位于路径: /my/path/

Here is the code (try/catch Java 6 style): 这是代码(try / catch Java 6样式):

InputStream is = null;
XMLStreamReader reader = null;
try {
    is = new FileInputStream(new File("/my/path/stuff.xml"));
    XMLInputFactory xif = XMLInputFactory.newInstance();
    reader = xif.createXMLStreamReader(is);
    ArrayDeque<String> nodes = new ArrayDeque<String>();
    while (reader.hasNext()) {
        int current = reader.next();
        switch (current) {
            case XMLStreamConstants.START_ELEMENT: {
                nodes.add(reader.getLocalName());
                System.out.println("START: " + nodes.getLast());
                if (nodes.size() > 1) {
                    Iterator<String> iterator = nodes.descendingIterator();
                    // skipping first one as it's already represented
                    iterator.next();
                    while (iterator.hasNext()) {
                        System.out.println("\t in " + iterator.next());
                    }
                }
                break;
            }
            case XMLStreamConstants.END_ELEMENT: {
                System.out.println("END: " + nodes.removeLast());
                Iterator<String> iterator = nodes.descendingIterator();
                while (iterator.hasNext()) {
                    System.out.println("\t in " + iterator.next());
                }
                break;
            }
        }
    }

}
        catch (FileNotFoundException fnfe) {
            fnfe.printStackTrace();
        }
        catch (XMLStreamException xse) {
            xse.printStackTrace();
        }
        finally {
            if (reader != null) {
                try {
                    reader.close();
                    is.close();
                }
                catch (XMLStreamException xse) {
                    xse.printStackTrace();
                }
                catch (IOException ioe) {
                    ioe.printStackTrace();
                }
            }
        }

Output: 输出:

START: stuff
START: node
     in stuff
START: tag
     in node
     in stuff
END: tag
     in node
     in stuff
END: node
     in stuff
START: way
     in stuff
START: nd
     in way
     in stuff
END: nd
     in way
     in stuff
START: tag
     in way
     in stuff
END: tag
     in way
     in stuff
END: way
     in stuff
END: stuff

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM