OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

 


 

   Re: [xml-dev] Java NPE at node.getAttribute( )

[ Lists Home | Date Index | Thread Index ]

Hi:
I tried using
NodeList nodeList=xmlDocument.selectNodes("/applet/transport/name", nsr);
XMLElement  node=(XMLElement)nodeList.item(0);
String trans_val1 = node.getAttribute("name"); 

But I still get a Java NPE at node.getAttribute("name").

I read about getAttribute( ) and it says --

public String getAttribute(String name)
Retrieves an attribute value by name. 
Parameters: 
name - The name of the attribute to retrieve. 
Returns: 
The Attr value as a string, or the empty string if that attribute does
not have a specified or default value.

Or
should I just use
NodeList nodeList=xmlDocument.selectNodes("/applet/transport/@name", nsr);
XMLElement  node=(XMLElement)nodeList.item(0);
String trans_val1 = node.getNodeValue(); 

On Mon, 24 Jan 2005 15:35:52 -0500, Christopher Maloney
<dude@chrismaloney.com> wrote:
> You're calling the getAttribute() method on an Attribute node.
> 
> NodeList nodeList=xmlDocument.selectNodes("/applet/transport/@name", nsr);
> 
> returns only Attribute nodes (since the last thing in your XPath expression is
> "@name".  Maybe you wanted the XPath expression "/applet/transport/name"?
> 
> Otherwise, you need to call the getValue() method on an Attribute node,
> not getAttribute().
> 
> Nishi Prafull wrote:
> 
> >Hi:
> >
> >If I have the following in my xml doc,
> ><tptd:applet xmlns  tptd="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"/>
> ><transport name="codebase"/>.....
> >.....
> ></tptd:applet>
> >
> >I want to extract the values of the "name" nodes and subsitute them as
> >values for the "name" nodes in another xml document. I have written
> >the following DOM Parser implementation but it gives me a Java NPE at
> >node.getAttribute().
> >Can someone please let me know what I am doing wrong here --
> >
> >public void doSub() throws FileNotFoundException, IOException,
> >XSLException, InvocationTargetException, XMLParseException,
> >SAXException
> >  {
> >   URL in_xml_url = new URL("file://" + getSpec());
> >   FileOutputStream out_xml = new FileOutputStream(m_outfileSpec, false);
> >
> >    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();
> >
> >   //namespace resolver
> >    NodeList nodeList=xmlDocument.selectNodes("/applet/transport/@name", nsr);
> >
> >    XMLElement  node=(XMLElement)nodeList.item(0);
> >    String trans_val1 = node.getAttribute("name"); //getting Java NPE here
> >    XMLElement  node1=(XMLElement)nodeList.item(1);
> >    String trans_val2 = node1.getAttribute("name");
> >
> >      //subsitute in the new document
> >      parser.parse(new FileReader(m_outfileSpec));
> >      XMLDocument document1 =parser.getDocument();
> >
> >      XMLElement nsr_new = (XMLElement) document1.getDocumentElement();
> >
> >      NodeList nodeList1=document1.selectNodes("/configuration//plus/transport/@name",f);
> >         XMLElement  node2=(XMLElement)nodeList1.item(0);
> >         node2.setAttribute("name",trans_val1);
> >         XMLElement  node3=(XMLElement)nodeList.item(1);
> >         node3.setAttribute("name",trans_val2);
> >         document1.print(out_xml);
> >         out_xml.close();
> >  }
> >
> >
> >Thanks much.
> >
> >-----------------------------------------------------------------
> >The xml-dev list is sponsored by XML.org <http://www.xml.org>, an
> >initiative of OASIS <http://www.oasis-open.org>
> >
> >The list archives are at http://lists.xml.org/archives/xml-dev/
> >
> >To subscribe or unsubscribe from this list use the subscription
> >manager: <http://www.oasis-open.org/mlmanage/index.php>
> >
> >
> >
> >
> >
> 
> -----------------------------------------------------------------
> The xml-dev list is sponsored by XML.org <http://www.xml.org>, an
> initiative of OASIS <http://www.oasis-open.org>
> 
> The list archives are at http://lists.xml.org/archives/xml-dev/
> 
> To subscribe or unsubscribe from this list use the subscription
> manager: <http://www.oasis-open.org/mlmanage/index.php>
> 
>




 

News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 2001 XML.org. This site is hosted by OASIS