Note that there are some explanatory texts on larger screens.

plurals
  1. POXPATH in Java: parent node missing
    primarykey
    data
    text
    <p>I have the following XML structure:</p> <pre><code>&lt;map name="testmap"&gt; &lt;definitions&gt; &lt;tile name="ground"&gt; &lt;!-- a normal tile that has no special obstacles --&gt; &lt;centralObstacle&gt;ground&lt;/centralObstacle&gt; &lt;neighbourObstacles&gt; &lt;north&gt;&lt;/north&gt; &lt;east&gt;&lt;/east&gt; &lt;south&gt;&lt;/south&gt; &lt;west&gt;&lt;/west&gt; &lt;/neighbourObstacles&gt; &lt;/tile&gt; &lt;tile name="wallE"&gt; &lt;!-- a ground tile with a wall obstacle at the east--&gt; &lt;centralObstacle&gt;ground&lt;/centralObstacle&gt; &lt;neighbourObstacles&gt; &lt;north&gt;&lt;/north&gt; &lt;east&gt;wall&lt;/east&gt; &lt;south&gt;&lt;/south&gt; &lt;west&gt;&lt;/west&gt; &lt;/neighbourObstacles&gt; &lt;/tile&gt; &lt;/definitions&gt; &lt;/map&gt; </code></pre> <p>And I would like to query it with XPATH. What I would like to do is get all the tile Nodes and then iterate over them to get all their names and other relevant information (using different XPATH queries).</p> <p>Because XPATH expressions are to be ran on a Document, I have used the following <code>nodeListToDoc()</code> function, provided in <a href="https://stackoverflow.com/questions/5786936/create-xml-document-using-nodelist">this</a> answer to convert the result of an XPATH query (a NodeList) to a Document. This way I can first get all the Tiles, then iterate over them to get Tile specific information.</p> <pre><code>private Document nodeListToDoc(NodeList nodes) throws ParserConfigurationException { Document newXmlDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); Element root = newXmlDocument.createElement("root"); newXmlDocument.appendChild(root); for (int i = 0; i &lt; nodes.getLength(); i++) { Node node = nodes.item(i); Node copyNode = newXmlDocument.importNode(node, true); root.appendChild(copyNode); } return newXmlDocument; } </code></pre> <p>What I first do is parse the file to a Document and then run the query to get a NodeList that contains all my Tiles. When I run the query <code>//definitions/tile</code> I get a NodeList containing two Node items (I have verified this), which is correct. The result of applying <code>nodeListToDoc()</code> looks like this.</p> <pre><code> &lt;?xml version="1.0" encoding="UTF-16"?&gt; &lt;root&gt;&lt;tile name="ground"&gt; &lt;!-- a normal tile that has no special obstacles --&gt; &lt;centralObstacle&gt;ground&lt;/centralObstacle&gt; &lt;neighbourObstacles&gt; &lt;north/&gt; &lt;east/&gt; &lt;south/&gt; &lt;west/&gt; &lt;/neighbourObstacles&gt; &lt;/tile&gt;&lt;tile name="wallE"&gt; &lt;!-- a ground tile with a wall obstacle at the east--&gt; &lt;centralObstacle&gt;ground&lt;/centralObstacle&gt; &lt;neighbourObstacles&gt; &lt;north/&gt; &lt;east&gt;wall&lt;/east&gt; &lt;south/&gt; &lt;west/&gt; &lt;/neighbourObstacles&gt; &lt;/tile&gt;&lt;/root&gt; </code></pre> <p>So far so good. Now things go bad. I want to iterate over the two nodes, make a NodeList of them, convert that NodeList to a Document and then run some queries over them. One of the queries is get the name of every tile. I thought the following code piece would do the trick:</p> <pre><code> for (int i = 0; i &lt; nodes.getLength(); i++) { // iterate over the two nodes NodeList tile = (NodeList) nodes.item(i); // create a nodelist containing only the first node Document attrdoc = nodeListToDoc(tile); // convert it to a document } </code></pre> <p>However, when I print the resulting tree that attrdoc represents, I get, for the first iteration, the following result:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-16"?&gt; &lt;root&gt; &lt;!-- a normal tile that has no special obstacles --&gt; &lt;centralObstacle&gt;ground&lt;/centralObstacle&gt; &lt;neighbourObstacles&gt; &lt;north/&gt; &lt;east/&gt; &lt;south/&gt; &lt;west/&gt; &lt;/neighbourObstacles&gt; &lt;/root&gt; </code></pre> <p>This is incorrect. The child of the root element should be tile? Where did this element go? </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload