Creating XML using DOM parser

Discussions

XML & Web services: Creating XML using DOM parser

  1. Creating XML using DOM parser (7 messages)

    Go thro' the following code.
    I expect the foll doc output

    <root>
      <transactionNm>
        <commandNm>logon.do</commandNm>
      </transactionNm>
    </root>

    But the SOP statements print NUL... Tried debudding... doc is being formed but after normalizing it beyond that point it is going awry.

    Pls help

    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.NamedNodeMap;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;

    public class XMLGenerator {

    public static void main(String args[]) {

    Document newdoc = null;
    try {

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();

    newdoc = db.newDocument();

    } catch (Exception e) {
    System.out.println("Exception: " + e.toString());
    System.exit(1);
    }

    // Creating a new document
    Element rootElmt = newdoc.createElement("root");
    Element tranaction = newdoc.createElement("tranactionNm");
    Element commandName = newdoc.createElement("commandName");

    commandName.appendChild(newdoc.createTextNode("logon.do"));

    tranaction.appendChild(commandName);
    rootElmt.appendChild(tranaction);
    newdoc.appendChild(newRoot);

    newdoc.normalize();

    System.out.println("commandName" + commandName.toString());
    System.out.println("tranaction" + tranaction.toString());
    System.out.println("Root" + newRoot.toString());
    System.out.println("Document: " + newdoc.getDocumentElement().toString());

    }

    }

    Threaded Messages (7)

  2. Creating XML using DOM parser[ Go to top ]

    Replace newRoot with rootElmt in your code and try.
  3. Creating XML using DOM parser[ Go to top ]

    I have done that... thats not the pblm... I wonder why it is throwing null when toString () is used.
  4. Creating XML using DOM parser[ Go to top ]

    You are assuming the toString() method of DOM nodes does something useful ... they don't. The toString() method is a Java method, not a DOM method, and DOM does not guarantee this method does anything functional.

    The simplest way to convert a DOM tree to a String in a standard way is to use the classes in the javax.xml.transform package and a "null" transform:


    // Create source and result objects
    Source source = new DOMSource(doc);
    StringWriter out = new StringWriter();
    Result result = new StreamResult(out);

    TransformerFactory tFactory =
        TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();
    transformer.transform(source, result);

    System.out.println(out.toString());


    For this to work, you will need to import:

    import java.io.StringWriter;
    import javax.xml.transform.Transformer;
    import javax.xml.transform.TransformerFactory;
    import javax.xml.transform.Source;
    import javax.xml.transform.Result;
    import javax.xml.transform.dom.DOMSource;
    import javax.xml.transform.stream.StreamResult;
  5. Creating XML using DOM parser[ Go to top ]

    That was indeed a very good solution to my problem. Thanks for correcting my misconception.

    I actually saw this chunk in a IBM tutorial. Does this mean that they would have had their custom toString() method to do the need ful?
  6. Creating XML using DOM parser[ Go to top ]

    I have no idea. Prior to JAXP, people had to use parser-specific features to convert DOM trees to strings; maybe the IBM tutorial was doing that.
  7. Creating XML using DOM parser[ Go to top ]

    Well, I used JDK1.4 and your program and it worked (without any modification to your code) However it may not work on JDK1.3 and below as JDK1.4 started shipping XML parsers. Ofcourse I agree with Paul's suggestion so that there is no ambiguity
  8. Creating XML using DOM parser[ Go to top ]

    That was quite informative Hari