2 Replies Latest reply: Jan 9, 2009 4:25 PM by 807589 RSS

    XML Parsing Problem: Nothing is showing up the way it should

    807589
      XML.java
      import javax.xml.parsers.*;
      import java.io.*;
      import org.w3c.dom.*;
      
      public class XML {
           public static void main(String[] args) {
                try {
                     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                     DocumentBuilder builder = factory.newDocumentBuilder();
                     File f = new File("Window.xml");
                     Document doc = builder.parse(f);
                     
                     //get XML information
                     Element root = doc.getDocumentElement();
                     NodeList children = root.getChildNodes();
                     System.out.println(root);
                     for(int i = 0; i < children.getLength(); i++) {
                          System.out.println(children.item(i));
                     }
                } catch(Exception ex) {
                     ex.printStackTrace();
                }
           }
      }
      Window.xml
      <window>
           <title>Window</title>
           <width>500</width>
           <height>500</height>
      </window>
        • 1. Re: XML Parsing Problem: Nothing is showing up the way it should
          807589
          Why is the Text not showing up?

          Oh and before I forget Here is the output:
          jacob-garbers-macbook-pro:Desktop jacobgarber$ java XML
          [window: null]
          [#text: 
               ]
          [title: null]
          [#text: 
               ]
          [width: null]
          [#text: 
               ]
          [height: null]
          [#text: 
          ]
          • 2. Re: XML Parsing Problem: Nothing is showing up the way it should
            807589
            It's further down in the DOM tree:
            import javax.xml.parsers.*;
            import java.io.*;
            import org.w3c.dom.*;
            
            public class XML {
                public static void main(String[] args) {
                    try {
                        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                        DocumentBuilder builder = factory.newDocumentBuilder();
                        File f = new File("Window.xml");
                        Document doc = builder.parse(f);
            
                        //get XML information
                        node(doc.getDocumentElement(), "");
                    } catch(Exception ex) {
                        ex.printStackTrace();
                    }
                }
            
                static void node(org.w3c.dom.Node node, String indent) {
                    System.out.println(indent + node);
                    indent += "  ";
                    NodeList children = node.getChildNodes();
                    for(int i = 0; i < children.getLength(); i++) {
                        node(children.item(i), indent);
                    }
                }
            }