Note that there are some explanatory texts on larger screens.

plurals
  1. POXML parser example
    primarykey
    data
    text
    <p>I am trying to parse the following XML:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;!-- _tag --&gt; &lt;tag value="0" actions_tagged="0" name="adjust temperature"&gt; &lt;link type="application/xml" rel="list" href="/api/v1/tags.xml"/&gt; &lt;link type="application/xml" rel="self" href="/api/v1/tags/adjust%20temperature.xml"/&gt; &lt;actions&gt; &lt;action id="105"&gt; &lt;link type="application/xml" rel="tagged_action" href="/api/v1/actions/105.xml"/&gt; &lt;short_name&gt;Set thermostat to 68F in the winter and 74F in the summer&lt;/short_name&gt; &lt;/action&gt; &lt;action id="106"&gt; &lt;link type="application/xml" rel="tagged_action" href="/api/v1/actions/106.xml"/&gt; &lt;short_name&gt;Close windows and blinds&lt;/short_name&gt; &lt;/action&gt; &lt;/actions&gt; &lt;/tag&gt; </code></pre> <p>I would like to capture each "action id" and each "short name". I'm able to capture the short name with the following code. But, how do you obtain the corresponding action id?</p> <pre><code>String action = null; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Actions myActions = new Actions(); InputSource inStream = new InputSource(); inStream.setCharacterStream(new StringReader(xml)); Document doc = db.parse(inStream); NodeList nodeList = doc.getElementsByTagName("action"); for (int s = 0; s &lt; nodeList.getLength(); s++) { Node fstNode = nodeList.item(s); if (fstNode.getNodeType() == Node.ELEMENT_NODE) { Element fstElmnt = (Element) fstNode; NodeList lstNmElmntLst = fstElmnt.getElementsByTagName("short_name"); Element lstNmElmnt = (Element) lstNmElmntLst.item(0); NodeList lstNm = lstNmElmnt.getChildNodes(); String currentAction = ((Node) lstNm.item(0)).getNodeValue(); if(currentAction != null) { myActions.setShort_name(currentAction); } lstNmElmntLst = fstElmnt.getElementsByTagName("action id"); // this didn't work - nor did 'id' lstNmElmnt = (Element) lstNmElmntLst.item(0); lstNm = lstNmElmnt.getChildNodes(); currentAction = ((Node) lstNm.item(0)).getNodeValue(); if(currentAction != null) { myActions.setDescription(currentAction); } } } </code></pre>
    singulars
    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