Forum Stats

  • 3,875,872 Users
  • 2,266,977 Discussions
  • 7,912,361 Comments

Discussions

XML Namespace handling?

843834
843834 Member Posts: 49,972
edited Jul 25, 2001 5:26AM in Java Technology & XML
Hi,
I am having a typical problem here. Is there any way I can set a namespace to an XML string before I convert it into a Document. The problem is I have this XML string and I am trying to parse it, but I get a java.io.Nullpointer Exception because one of the tags in the XML string has a name space and I need to assign a name space to it here.

here's an example...

Following is my XML string say....

<metadata>
<schema>Schema 1</schema>
<schemaversion>DX:MD1.0 S:1.0</schemaversion>
<dlmd:record>Record</dlmd:record>
</metadata>

I need to parse this string, but I can not do it without setting a namespace for dlmd:record! Or is there a way I can do it without the namespace...I tried using the setNamespaceaware() option but it does not seem to work.

It's sort of strange as to why I need to assign a name space to it here...but thats the problem I have to deal with...

any help would be very much appreciated.

Thanks in advance
Prashanth
«1

Comments

  • 843834
    843834 Member Posts: 49,972
    I agree with your hunch. The parser should treat dlmd:record as just another element (colons are allowed in XML element names when namspace feature is turned off). Maybe you could elaborate a bit on what parser you're using, what Document (JDOM or DOM) you're building and where the exception comes from (io exception is kind of strange, too).
  • 843834
    843834 Member Posts: 49,972
    Hi
    I am using JAXP 1.1 and I am builidng a DOM document. The exception I get is a SAX Exception and the following is the stack trace

    java.lang.NullPointerException
    at org.apache.crimson.parser.Parser2.parseInternal(Parser2.java:523)
    at org.apache.crimson.parser.Parser2.parse(Parser2.java:304)
    at org.apache.crimson.parser.XMLReaderImpl.parse(XMLReaderImpl.java:433)
    at org.apache.crimson.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:179)

    at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:161)
    at dlnet.DLXMLParse.dlDocumentBuild(DLXMLParse.java:282)
    at dlnet.DLXMLParse.dlStart_Parse(DLXMLParse.java:50)
    at dlnet.DLTrigger.main(DLTrigger.java:40)

    Hope you can suggest a solution.....
  • 843834
    843834 Member Posts: 49,972
    I see. I only tried xerces with your example and it was just fine. You're using Crimson, though and there seems to be a bug in crimson1.1. Go, get crimson1.1.1 from http://xml.apache.org and it'll parse fine.

    Good luck,
    Leo.
  • 843834
    843834 Member Posts: 49,972
    Using the new version of Crimson does not help either. After I build the new Document Factory I set the namespace awareness to false and yet it gives me a sax exception......Could something else be wrong too!??

    Thanks for your help....
    Prashanth
  • 843834
    843834 Member Posts: 49,972
    Prashanth,
    this is what I'm doing. First, the parser test code:
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import org.w3c.dom.Document;
    
    public class JAXPDOMTest {
    
        public void domParse(String url) {
            DocumentBuilder parser;
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setValidating(false);
            factory.setNamespaceAware(false);
    
            try {
                parser = factory.newDocumentBuilder();
                Document doc = parser.parse(url);
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        static public void main(String[] args)
        {
           JAXPDOMTest x=new JAXPDOMTest();
           x.domParse("test.xml");
        }
    }
    and here's test.xml:
    <?xml version="1.0"?>
    
    <metadata>
       <schema>Schema 1</schema>
       <schemaversion>DX:MD1.0 S:1.0</schemaversion>
       <dlmd:record>Record</dlmd:record>
    </metadata>
    jaxp-1.1 is my directory containing the official 1.1 release and my complete classpath look like this:
    CLASSPATH=d:\xml_tools\jaxp-1.1\crimson.jar;d:\xml_tools\jaxp-1.1\jaxp.jar;.

    I run the above (java JAXPDOMTest), I get:
    java.lang.NullPointerException
    at org.apache.crimson.parser.Parser2.parseInternal(Parser2.java:523)
    at org.apache.crimson.parser.Parser2.parse(Parser2.java:304)
    at org.apache.crimson.parser.XMLReaderImpl.parse(XMLReaderImpl.java:433)

    at org.apache.crimson.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:179)
    at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:134)
    at JAXPDOMTest.domParse(JAXPDOMTest.java:15)
    at JAXPDOMTest.main(JAXPDOMTest.java:24)

    I set the class path to:
    CLASSPATH=d:\xml_tools\crimson-1.1.1\crimson.jar;d:\xml_tools\crimson-1.1.1\jaxp.jar;.

    and, voila, no exception.

    Hope that helps,
    Leo.
  • 843834
    843834 Member Posts: 49,972
    Hi

    This is what I did just now....I created a file out of the pseudo string and passed it to be parsed with crimson.....it gives me an error irrespective of whether I turn Namespace awareness true or not!

    What could the reason be?

    Thanks
    Prashanth
  • 843834
    843834 Member Posts: 49,972
    Well, that seems to be classic dilemma. The same code works fine on my machine, but has trouble on yours. I'm running on 95 with JSDK 1.3 (build 1.3.0-C).
    I'm afraid I can't help you any further, since I can't reproduce this behaviour.

    Leo.
  • 843834
    843834 Member Posts: 49,972
    Thanks anyways....guess I have to figure it out myself.
    Prashanth
  • 843834
    843834 Member Posts: 49,972
    It finally worked!!!! This is what I did. I had to delete the jar files in the \lib\ext directory of Java Runtime Environment and it started working. Thanks Leo you have been of great help.......

    Prashanth
  • 843834
    843834 Member Posts: 49,972
    Yeah, that's a valuable lesson. jar files in lib/ext, I believe, are accessed before stuff in the class path. Glad to hear it's working.
This discussion has been closed.