SAX Parser for creating (not parsing) XML document

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Naresh Agarwal

    SAX Parser for creating (not parsing) XML document

    Hi

    I have been using DOM parser to create XML documents. I want to use an
    alternate mechanism to create XML document as size of my XML document
    is large and I don;t want the overhead of DOM (where entire tree in
    constructed in memory).

    Like SAX APIs for parsing XML documents, Is there any thing like SAX
    parser/APIs for creating XML documents?

    Is there any other standard mechanism of creating XML document,
    without requiring the entire tree structure to be constructed in
    memory?

    Thanks,
    Naresh
  • Martin Honnen

    #2
    Re: SAX Parser for creating (not parsing) XML document

    Naresh Agarwal wrote:
    Hi
    >
    I have been using DOM parser to create XML documents. I want to use an
    alternate mechanism to create XML document as size of my XML document
    is large and I don;t want the overhead of DOM (where entire tree in
    constructed in memory).
    >
    Like SAX APIs for parsing XML documents, Is there any thing like SAX
    parser/APIs for creating XML documents?
    Java 6 has XMLStreamWriter



    --

    Martin Honnen

    Comment

    • Naresh Agarwal

      #3
      Re: SAX Parser for creating (not parsing) XML document

      On Jul 21, 4:09 pm, Martin Honnen <mahotr...@yaho o.dewrote:
      Naresh Agarwal wrote:
      Hi
      >
      I have been using DOM parser to create XML documents. I want to use an
      alternate mechanism to create XML document as size of my XML document
      is large and I don;t want the overhead of DOM (where entire tree in
      constructed in memory).
      >
      Like SAX APIs for parsing XML documents, Is there any thing like SAX
      parser/APIs for creating XML documents?
      >
      Java 6 has XMLStreamWriter http://www.java2s.com/Tutorial/Java/0440__XML/UsingXMLStreamW ritertoc...
      >
      --
      >
      Martin Honnen
      http://JavaScript.FAQTs.com/
      Thanks. Is there any thing available for this with Java 1.5, any open
      source app?

      Best
      Naresh

      Comment

      • Stanimir Stamenkov

        #4
        Re: SAX Parser for creating (not parsing) XML document

        Mon, 21 Jul 2008 21:46:42 -0700 (PDT), /Naresh Agarwal/:
        On Jul 21, 4:09 pm, Martin Honnen <mahotr...@yaho o.dewrote:
        >
        >Java 6 has XMLStreamWriter http://www.java2s.com/Tutorial/Java/0440__XML/UsingXMLStreamW ritertoc...
        >
        Thanks. Is there any thing available for this with Java 1.5, any open
        source app?
        <http://en.wikipedia.or g/wiki/StAXlists three implementations :
        * http://stax.codehaus.org/ Reference Implementation
        * Woodstox Open source StAX implementation
        * https://sjsxp.dev.java.net is Sun's Stax implementation
        --
        Stanimir

        Comment

        • Tony Lavinio

          #5
          Re: SAX Parser for creating (not parsing) XML document

          Stefan Ram wrote:
          Martin Honnen <mahotrash@yaho o.dewrites:
          >Java 6 has XMLStreamWriter
          >
          Often, people use »tag« for »element«.
          >
          Here, it seems to be the other way round:
          >
          xtw.writeStartE lement("http://www.w3.org/TR/REC-html40", "a");
          xtw.writeAttrib ute("href", "http://www.java2s.com" );
          xtw.writeCharac ters("here");
          xtw.writeEndEle ment();
          >
          . It seems as »writeEndElemen t« is supposed
          to write the end /tag/, i.e., "</a>".
          >
          So, more correct names to me would be:
          >
          »writeEndTag()« ,
          »writeEndOfElem ent()«, or
          »writeElementEn ding()«.
          You're not writing tags with this, you're writing /events/.

          So writeStartEleme nt means you are sending the StartElement
          /event/ into the stream. writeEndElement means you are
          sending the EndElement /event/ into the stream.

          When using XMLStreamWriter , you must remember that you are
          acting as the parser.


          --
          Tony Lavinio <DataDirect <Stylus Studio XML <alavinio@progre ss.com
          XQuery, XSLT, XML Schema and EDI Toolset <http://www.stylusstudio.com/
          <There is no problem that brute force and ignorance cannot overcome <>

          Comment

          • Stanimir Stamenkov

            #6
            Re: SAX Parser for creating (not parsing) XML document

            22 Jul 2008 16:31:54 GMT, /Stefan Ram/:
            Another way to write XML with pre-1.6-Java:
            >
            com.sun.org.apa che.xml.interna l.serialize.XML Serializer serializer =
            new com.sun.org.apa che.xml.interna l.serialize.XML Serializer
            ( fileOutputStrea m, outputFormat);
            serializer.star tDocument();
            { serializer.star tElement("", "example", "example",
            new com.sun.org.apa che.xml.interna l.serializer.
            AttributesImplS erializer() );
            { final char[] data = "test text" . toCharArray();
            serializer.char acters( data, 0, data.length );
            serializer.endE lement( "", "example", "example" ); }
            serializer.endD ocument(); }}}
            Here's more standard way of doing it without requiring
            implementation specific classes (works with Java 1.4):



            --
            Stanimir

            Comment

            • Philippe Poulard

              #7
              Re: SAX Parser for creating (not parsing) XML document

              Stefan Ram a écrit :
              Naresh Agarwal <naresh.agarwal @gmail.comwrite s:
              >Is there any thing available for this with Java 1.5, any open
              >source app?
              >
              Writing XML is simpler than reading XML.
              >
              So, sometimes, one can get by with no library at all:
              >
              outStream.print ( "<" );
              outStream.print ( type );
              outStream.print ( ">" );
              >
              hi,

              IMHO, this is one of the worst practice

              outStream.print ( "<" );
              outStream.print ( type );
              outStream.print ( ">" );
              outStream.print ( "if (a<b ) {}" );

              you'll have too many occasions to break your XML result ; rely on tools
              made for that purpose that will produce well-formed XML

              you can also use templates for creating SAX documents with RefleX :

              of course, you can loop on whatever you want to create content (merge
              thousand XML files for example:
              http://reflex.gforge.inria.fr/tips.h...rsingFragments )

              then pipe the created doc to an XSLT serializer :


              you can use it from the command line, embed it in a program, or deploy
              it within a web server

              tutorials are available here :


              I will talk about all that stuff at Balisage 2008 in Montreal, with a
              focus on schema languages


              --
              Cordialement,

              ///
              (. .)
              --------ooO--(_)--Ooo--------
              | Philippe Poulard |
              -----------------------------

              Have the RefleX !

              Comment

              Working...