Loading ...
Sorry, an error occurred while loading the content.

Java String to org.w3c.dom.Document conversion

Expand Messages
  • Velusamy Velu
    Hi: We create XML documents using Castor to feed to the STxx framework for XSL transformation in to HTML/PDF. The Marshaller from Castor creates the XML
    Message 1 of 2 , Aug 25, 2004
    • 0 Attachment
      Hi:

      We create XML documents using Castor to feed to the STxx framework
      for XSL transformation in to HTML/PDF. The Marshaller from Castor
      creates the XML string. It works as expected.

      We then try to use the DOMParser to convert the string to an instance
      of org.w3c.dom.Document. The conversion process appear to run OK but
      the Document created is null.

      Did any of you ran in to this problem? It may also help me if we know
      what is the quickest and the easiest way to convert a Java String to
      an XML document? Any help would be greatly appreciated.

      Following is the sample code that demonstrates a problem we are
      running in to.

      import java.io.StringReader;
      import org.apache.xerces.parsers.DOMParser;
      import org.w3c.dom.Document;
      import org.xml.sax.InputSource;

      public class CastorTest
      {

      public static void main(String[] args)
      {
      final String xmlString =
      "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
      + "<person><name>Jane Smith</name></person>";
      try
      {
      InputSource source = new InputSource(new StringReader
      (xmlString));
      DOMParser parser = new DOMParser();
      parser.parse(source);
      Document document = parser.getDocument();
      System.out.println(document.toString());
      }
      catch (Exception e)
      {
      e.printStackTrace();
      }
      }
      }

      Thanks
      Velu
    • pcarey@lexmark.com
      This works for me... import java.io.StringReader; import org.w3c.dom.Document; import org.xml.sax.InputSource; import javax.xml.parsers.DocumentBuilderFactory;
      Message 2 of 2 , Aug 25, 2004
      • 0 Attachment
        This works for me...

        import java.io.StringReader;
        import org.w3c.dom.Document;
        import org.xml.sax.InputSource;
        import javax.xml.parsers.DocumentBuilderFactory;
        import javax.xml.parsers.DocumentBuilder;

        public class CastorTest
        {
        ...
        ...

        try{
        InputSource source = new InputSource(new StringReader(xmlString));
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document d = builder.parse(source);
        System.out.println(d); //org.apache.crimson.tree.XmlDocument@4f1d0d
        System.out.println(d.getDocumentElement().getTagName()); //<person>
        }

        ...
        ...


        |---------+---------------------------->
        | | "Velusamy Velu" |
        | | <vvelu@...>|
        | | |
        | | 08/25/2004 09:11 |
        | | AM |
        | | Please respond to|
        | | XSL-FO |
        | | |
        |---------+---------------------------->
        >------------------------------------------------------------------------------------------------------------------------------|
        | |
        | To: XSL-FO@yahoogroups.com |
        | cc: |
        | Subject: [XSL-FO] Java String to org.w3c.dom.Document conversion |
        >------------------------------------------------------------------------------------------------------------------------------|




        Hi:

        We create XML documents using Castor to feed to the STxx framework
        for XSL transformation in to HTML/PDF. The Marshaller from Castor
        creates the XML string. It works as expected.

        We then try to use the DOMParser to convert the string to an instance
        of org.w3c.dom.Document. The conversion process appear to run OK but
        the Document created is null.

        Did any of you ran in to this problem? It may also help me if we know
        what is the quickest and the easiest way to convert a Java String to
        an XML document? Any help would be greatly appreciated.

        Following is the sample code that demonstrates a problem we are
        running in to.

        import java.io.StringReader;
        import org.apache.xerces.parsers.DOMParser;
        import org.w3c.dom.Document;
        import org.xml.sax.InputSource;

        public class CastorTest
        {

        public static void main(String[] args)
        {
        final String xmlString =
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
        + "<person><name>Jane Smith</name></person>";
        try
        {
        InputSource source = new InputSource(new StringReader
        (xmlString));
        DOMParser parser = new DOMParser();
        parser.parse(source);
        Document document = parser.getDocument();
        System.out.println(document.toString());
        }
        catch (Exception e)
        {
        e.printStackTrace();
        }
        }
        }

        Thanks
        Velu





        Yahoo! Groups Links
      Your message has been successfully submitted and would be delivered to recipients shortly.