Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

XMLDocument.selectNodes

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
N Bhonsle
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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");

}
 
And inside of my fortune cookie was this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic