Hendrik Jander
Hendrik Jander

Reputation: 5725

JAXP and StAX - DOM Node is represented as Document

I want to read a potentially big XML file. For memory efficiency and ease of processing i want use a mixture of StAX and DOM as described here.

My input looks as follows (it is OSM)

<?xml version="1.0" encoding="UTF-8"?>
<osm version="0.6" generator="CGImap 0.0.2">
 <bounds minlat="54.0889580" minlon="12.2487570" maxlat="54.0913900" maxlon="12.2524800"/>
 <node id="298884269" lat="54.0901746" lon="12.2482632" user="SvenHRO" uid="46882" visible="true" version="1" changeset="676636" timestamp="2008-09-21T21:37:45Z"/>
 <node id="261728686" lat="54.0906309" lon="12.2441924" user="PikoWinter" uid="36744" visible="true" version="1" changeset="323878" timestamp="2008-05-03T13:39:23Z"/>
 <node id="1831881213" version="1" changeset="12370172" lat="54.0900666" lon="12.2539381" user="lafkor" uid="75625" visible="true" timestamp="2012-07-20T09:43:19Z">
  <tag k="name" v="Neu Broderstorf"/>
  <tag k="traffic_sign" v="city_limit"/>
 </node>
 ...
 <node id="298884272" lat="54.0901447" lon="12.2516513" user="SvenHRO" uid="46882" visible="true" version="1" changeset="676636" timestamp="2008-09-21T21:37:45Z"/>
 <way id="26659127" user="Masch" uid="55988" visible="true" version="5" changeset="4142606" timestamp="2010-03-16T11:47:08Z">
  <nd ref="292403538"/>
  <nd ref="298884289"/>
  ...
  <nd ref="261728686"/>
  <tag k="highway" v="unclassified"/>
  <tag k="name" v="Pastower Straße"/>
 </way>

While processing i want to check for Node names like “way” or “relation”(like here)

My code looks as follows:

    xsr.nextTag();
    while(xsr.nextTag() == XMLStreamConstants.START_ELEMENT) {

        DOMResult result = new DOMResult();
        t.transform(new StAXSource(xsr), result);
        Node domNode = result.getNode();

        System.out.println(domNode.getNodeName());
    }

My problem is that the extracted nodes are represented as documents instead of nodes. Meaning my debug output shows:

#document #document #document

for every line. The expected output would be "node", "way" or "relation".

When i parse the file and then print out the Nodes as described here they are printed out correctly.

I am a newbie to StAX and DOM so any hints would be very helpful.

I am using Mac OS X 10.10.2 with Java 1.8.25.

Upvotes: 0

Views: 249

Answers (2)

Santhosh Kumar Tekuri
Santhosh Kumar Tekuri

Reputation: 3020

change:

System.out.println(domNode.getNodeName());

to:

System.out.println(((Document)domNode).getDocumentElement().getNodeName());

Upvotes: 1

Michael Kay
Michael Kay

Reputation: 163458

Well, firstly, your terminology is confusing. I think when you say "node" you mean "element node". The document is a node as well.

The result of an XSLT transformation is a tree, and the tree is always rooted at a document node (in XSLT 1.0 this is simply called a "root node"). You will find the element node you are looking for as a child of the document/root node.

Upvotes: 1

Related Questions