| Author |
XMLDocument.selectNodes
|
N Bhonsle
Greenhorn
Joined: Jan 12, 2005
Posts: 13
|
|
Hi: If I have the following in my xml doc, <tptd:applet xmlns isco="http://www.oracle.com/tptd/configuration" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.oracle.com/tptd/configuration plus _config.xsd"> <transport name="jrmp"/>^M <transport name="codebase"/>^M..... ..... </tptd:applet> Will the following return me all the "name" nodes -- NodeList nodeList=XMLDocument.selectNodes("/applet/transport" ); Please let me know. Thanks.
|
 |
Lasse Koskela
author
Sheriff
Joined: Jan 23, 2002
Posts: 11962
|
|
Have you tried? I don't know if this "XMLDocument" class is namespace-aware or not and how its XPath implementation works. Then again, I have no idea which API or product that class belongs to?
|
Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
|
 |
Madhav Lakkapragada
Ranch Hand
Joined: Jun 03, 2000
Posts: 5040
|
|
Originally posted by N Bhonsle: Hi: If I have the following in my xml doc, <tptd:applet xmlns  isco="http://www.oracle.com/tptd/configuration" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.oracle.com/tptd/configuration plus _config.xsd"> <transport name="jrmp"/>^M <transport name="codebase"/>^M..... ..... </tptd:applet> Will the following return me all the "name" nodes -- NodeList nodeList=XMLDocument.selectNodes("/applet/transport" ); Please let me know. Thanks.
IMO that returns the list of 'transport' elements as opposed to the 'name' attributes ? Not very positive about parsers but atleast that's the case with the XSL when you use that XPath (and so I have some amount of confidence in what I say). - m
|
Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
|
 |
N Bhonsle
Greenhorn
Joined: Jan 12, 2005
Posts: 13
|
|
Hi, I tried the following to select the name attributes of the 2 transport nodes -- NodeList nodeList=xmlDocument.selectNodes("/applet/transport/@name" ); for(int i=0; i<nodeList.getLength(); i++) { XMLElement node=(XMLElement)nodeList.item(i); node.getAttribute("name"); } XMLElement node=(XMLElement)nodeList.item(0); node.getAttribute("name"); XMLElement node1=(XMLElement)nodeList.item(1); node1.getAttribute("name"); But I get a NPE at the first node.getAttribute("name"); I think I need to use NodeList nodeList=xmlDocument.selectNodes("/applet/transport/@name",nsr); where nsr is the NSResolver to resolve any prefixes that occur in given pattern. How can I get the value for nsr? Thanks.
|
 |
N Bhonsle
Greenhorn
Joined: Jan 12, 2005
Posts: 13
|
|
Sorry, my mistake in one of the calls for the node object below -- for(int i=0; i<nodeList.getLength(); i++) { XMLElement node=(XMLElement)nodeList.item(i); } XMLElement node=(XMLElement)nodeList.item(0); String val1 = node.getNodeValue(); XMLElement node1=(XMLElement)nodeList.item(1); String val2 = node1.getNodeValue(); But I get a NPE at the first String val1 = node.getNodeValue(); I think I need to use NodeList nodeList=xmlDocument.selectNodes("/applet/transport/@name",nsr); where nsr is the NSResolver to resolve any prefixes that occur in given pattern. How can I get the value for nsr? Thanks.
|
 |
N Bhonsle
Greenhorn
Joined: Jan 12, 2005
Posts: 13
|
|
Hi: I realised that XMLElement does not the getNodeValue() methods, so this is the corrected version. I have included the computation of the namespace resolver too but I get a Java NPE at node.getAttribute()? Can someone tell me why? I tried the following but I am getting NPE at node.getAttribute(), can you tell me whether my namespace resolver is being computed incorrectly?-- public void doCombine() throws FileNotFoundException, IOException, XSLException, InvocationTargetException, XMLParseException, SAXException { URL in_xml_url = new URL("file://" + getSpec()); XMLDocument xmlDocument = null; //Create a parser DOMParser parser = new DOMParser(); parser.setValidationMode(XMLConstants.NONVALIDATING); parser.setBaseURL(in_xml_url); //Create a document from the url parser.parse(in_xml_url); //Cache the document from the parser xmlDocument = parser.getDocument(); XMLElement nsr = (XMLElement) xmlDocument.getDocumentElement(); NodeList nodeList=xmlDocument.selectNodes("/applet/transport/@name", nsr); XMLElement node=(XMLElement)nodeList.item(0); String trans_val1 = node.getAttribute("name"); //get NPE here XMLElement node1=(XMLElement)nodeList.item(1); String trans_val2 = node1.getAttribute("name"); }
|
 |
 |
|
|
subject: XMLDocument.selectNodes
|
|
|