3 Replies Latest reply: Apr 30, 2010 7:22 AM by JoachimSauer RSS

    Cannot parse node in XML file

    807580
      I have the following xml file:

      <?xml version="1.0" encoding="UTF-8"?>
      <model:test xmlns:model="bla">
        <Bounds>
          <Left>0.0</Left>
          <Top>0.0</Top>
          <Width>0.0</Width>
          <Height>0.0</Height>
        </Bounds>
      </model:test>
      Now I try to parse this using the below code:
          DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
          domFactory.setNamespaceAware(true);
          DocumentBuilder builder = null;
          try {
            builder = domFactory.newDocumentBuilder();
          } catch (ParserConfigurationException e) {
            e.printStackTrace();
          }
          
          try {
            doc = builder.parse(new File(pathToData));
          } catch (SAXException e) {
            e.printStackTrace();
          } catch (IOException e) {
            e.printStackTrace();
          }
          
          
          NodeList nodes = doc.getElementsByTagNameNS("model","test");
          Node node = nodes.item(0);
          if(node == null) {
            System.out.println("is null");
          }    
          System.out.println(nodes.getLength());  
      But the null test evaluates to true and it prints:

      is null

      I don't get any exceptions and also specify:

      domFactory.setNamespaceAware(true);


      so why is the node null?
        • 1. Re: Cannot parse node in XML file
          JoachimSauer
          The problem is here:
          NodeList nodes = doc.getElementsByTagNameNS("model","test");
          "model" has absolutely no meaning in the DOM, it's simply a short name for the full URI of the namespace you use ("bla" in your example). You need to use this instead:
              NodeList nodes = doc.getElementsByTagNameNS("bla","test");
          To demonstrate the fact, the following XML would be effectively equivalent to the one you posted
          <?xml version="1.0" encoding="UTF-8"?>
          <foobar:test xmlns:foobar="bla">
            <Bounds>
              <Left>0.0</Left>
              <Top>0.0</Top>
              <Width>0.0</Width>
              <Height>0.0</Height>
            </Bounds>
          </foobar:test>
          • 2. Re: Cannot parse node in XML file
            807580
            Thanks it now works. But if I want to print the Node as a xml string like:
                NodeList nodes = doc.getElementsByTagNameNS("bla","test");
                Node node = nodes.item(0);
                System.out.println(node.toString());
            I just get:

            [model:test: null]

            I could iterate the Node and prints its children values, but are there no "toXml()" method that can be called on a Node or a transformed Node?
            • 3. Re: Cannot parse node in XML file
              JoachimSauer
              You will have to use a Transformer (from a TransformerFactory) to transform a DOMSource (built with your element) to a StreamResult (built with a StringWriter).

              No, unfortunately I'm not kidding.