Project

Profile

Help

Getting attributes from a NodeInfo

Added by Anonymous almost 19 years ago

Legacy ID: #3463191 Legacy Poster: Daniela (danny_kay)

Okay, I've got an XML file that contains the data for HTTPConnections. I can access all the other values just fine, but I've got problems getting the attributes of the postvar element: <website> <page id="1" getcookie="true" sendcookie="false" requestmethod="POST"> <url><![CDATA[https://www.foobar.de/]]></url> <postvar type="username" name="benutzer_kennung" value="" /> <postvar type="password" name="benutzer_passwort" value="" /> <postvar name="aktion" value="login" /> <postvar name="aktion" value="login" /> <postvar name="sprach_id_webseite" value="1" /> </page> This is the code I use to fetch the postvar element from the page element with the id "this.id". InputSource is = new InputSource(new File("config/" + this.fileName + ".xml").toURL().toString()); SAXSource ss = new SAXSource(is); XPathEvaluator xpe = new XPathEvaluator(ss); List results = xpe.evaluate("/website/page[@id=" + this.id + "]/postvar"); for (Iterator iter = results.iterator(); iter.hasNext();) { NodeInfo nodeInfo = (NodeInfo)iter.next(); } Inside of the for loop I've got the postvar element as a NodeInfo, but now I need to access its attributes. I've seen something that involves setting the NodeInfo as the context node, but I couldn't make that work. Can sombody please show me how to make it work. Thank you Daniela


Replies (2)

RE: Getting attributes from a NodeInfo - Added by Anonymous almost 19 years ago

Legacy ID: #3463537 Legacy Poster: Michael Kay (mhkay)

One way to do it, of course, is to evaluate another XPath expression, namely "@*", with the postvar element as the context node. The more direct way to get all the attributes is using node.iterateAxis(Axis.ATTRIBUTE), which returns a SequenceIterator over the attribute nodes. If you know the name of the attribute you want to access, you can use the static helper method Navigator.getAttributeValue(elementNode, uri, local-name) Michael Kay

RE: Getting attributes from a NodeInfo - Added by Anonymous almost 19 years ago

Legacy ID: #3463640 Legacy Poster: Daniela (danny_kay)

Wow, the "Fastest Response On An Internet Forum Award" definitely goes to you, Michael! Since I know exactly, which attributes I want, I used the second solution. I'll post the full code: <website> <page id="1" getcookie="true" sendcookie="false" requestmethod="POST"> <url><![CDATA[https://www.foobar.de/]]></url> <postvar type="username" name="benutzer_kennung" value="" /> <postvar type="password" name="benutzer_passwort" value="" /> <postvar name="aktion" value="login" /> <postvar name="aktion" value="login" /> <postvar name="sprach_id_webseite" value="1" /> </page> This is the code I use to fetch the postvar element from the page element with the id "this.id". InputSource is = new InputSource(new File("config/" + this.fileName + ".xml").toURL().toString()); SAXSource ss = new SAXSource(is); XPathEvaluator xpe = new XPathEvaluator(ss); List results = xpe.evaluate("/website/page[@id=" + this.id + "]/postvar"); for (Iterator iter = results.iterator(); iter.hasNext();) { NodeInfo nodeInfo = (NodeInfo)iter.next(); String name = Navigator.getAttributeValue(line, line.getURI().toString(), "name"); String type = Navigator.getAttributeValue(line, line.getURI().toString(), "type"); String value = Navigator.getAttributeValue(line, line.getURI().toString(), "value"); } Thank you for your help Daniela

    (1-2/2)

    Please register to reply