Performance sax vs dom

Hi All,
I have build a xml interface for a java application.
I parse the xml files using dom parser, get the data
and pass it to the java application. I know that DOM
builds a in memory tree of the xml file.
Will this degrade the performance of my application.
Will it be better to use a SAX parser instead?
Regards
Milind

It really depends on the size of the data that you are parsing. Parsing big XML files with DOM means you have a big tree in memory using up lots of memory and CPU. With SAX it will just take more time to process the XML than it would a smaller file.
The only way to know for sure if it makes a difference for you is to try both approaches using live data. It doesn't help to profile them if you are using a test XML file that is half the size of what you are planning on using in production.
So, the answer to your question is "It depends".

Similar Messages

  • In Java Mapping Whether to SAX or DOM ?

    While implementing Java Mapping which of the following SAX , DOM parsers  is the most efficient way to implement it.
    I mean considering the voulume of data in the XML format is large which  usage will give me most  optimal performance .
    It would be nice if someone could give a sample most  optimal java code using JAXP where  input stream is read  and after  processing written to  output stream.
    regards
    Nilesh Taunk.

    Hi Nilesh,
    >>>While implementing Java Mapping which of the following SAX , DOM parsers is the most efficient way to implement it.
    there's no one good answer fot this question
    except: it depends:)
    SAX and DOM are a little different parsers
    so it depends what will your java mapping
    have to do: if it will change the
    structure or maybe it will only change a few tags,
    or maybe it will do something else
    have a look at the page below to read a simple
    comparision between SAX and DOM
    http://www.scit.wlv.ac.uk/~jphb/cp2101/week4/XML_Parsing.html
    or this thread:
    http://www.biglist.com/lists/xsl-list/archives/200301/msg01318.html
    they will give you some idea which one to choose
    depending on your requirements
    BTW
    remember use java mappings only
    if you cannot use graphical mapping (very easy debugging)
    and if your document cannot be parsed (maybe huge documents)
    with XSLT which is supported by many graphilal tools
    Regards,
    michal
    <a href="/people/michal.krawczyk2/blog/2005/06/28/xipi-faq-frequently-asked-questions">XI FAQ - Frequently Asked Questions</a>

  • XML - SAX or DOM?

    Hi folks,
    I'm on the verge of emotional breakdown trying to get my head around working with XML in Java, and I would really appreciate a bit of help.
    What I would like to do is to store database metadata in an XML file.
    The steps I have taken so far are to read the metadata into a structure of objects (a metadata object that containts arraylists of tables objects that contain arraylists of columns object. Each of these object types holds information about the table or column concerned (table name, column type etc).
    I'm stuck when it comes to deciding whether to use SAX or DOM to write / read these files.
    Now, I understand that SAX is for sequential access, and DOM is for situations where you'd like to manipulate the XML tree. I guess that, given the nature of my data structure DOM would make more sense? Then I can define a node for each table, then off them, define a node for each column... Or am I missing the point here?
    Can anyone give me a nudge in the right direction?
    Thanks in advance
    s.

    Thanks for replying!
    Duffymo, I'd rather keep the xml content to the data
    to make the files easy to edit / write by hand.Not following you here.
    I understand that you want XML files, but XStream can take them and turn them into Java objects for you. Conversely, you can take a graph of Java objects and serialize them as XML.
    No worries about DOM or SAX that way.
    %

  • Xerces with SAX or DOM

    I am using xerces (in jar form) to parse an XML file.
    I've now read, after getting this to work, that xerces can use SAX or DOM.
    How do I know which one I am using?

    Actually, I have never used SAX for modifying attributes, but there is a lot of documentation available.
    http://java.sun.com/webservices/docs/1.1/tutorial/doc/JAXPSAX.html#wp69937
    I think this is a good example:
    import java.io.*;
    import org.xml.sax.*;
    import org.xml.sax.helpers.*;
    import org.apache.xerces.parsers.SAXParser;
    public class Trace extends DefaultHandler {
      int indent;
      void printIndent() {
        for (int i=0; i<indent; i++) System.out.print("-");
      public void startDocument() {
        System.out.println("start document");
      public void endDocument() {
        System.out.println("end document");
      public void startElement(String uri, String localName,
                               String qName, Attributes attributes) {
        printIndent();
        System.out.println("starting element: " + qName);
        indent++;
      public void endElement(String uri, String localName,
                             String qName) {
        indent--;
        printIndent();
        System.out.println("end element: " + qName);
      public void ignorableWhitespace(char[] ch, int start, int length) {
        printIndent();
        System.out.println("whitespace, length " + length);
      public void processingInstruction(String target, String data) {
        printIndent();
        System.out.println("processing instruction: " + target);
      public void characters(char[] ch, int start, int length){
        printIndent();
        System.out.println("character data, length " + length);
      public static void main(String[] args) {
        Trace t = new Trace();
        SAXParser p = new SAXParser();
        p.setContentHandler(t);
        try { p.parse(args[0]); }
        catch (Exception e) {e.printStackTrace();}
    }

  • SAX and DOM - treating encoding differently ??

    Hello!
    I have run into a strange problem - I was parsing my xml document using
    WebLogic's DOM and SAX parsers to compare their performance, and I found
    that while using their DOM parser I was able to parse the document just
    fine, using the SAX parser gave me an error:
    org.xml.sax.SAXParseException: Declared encoding "UTF-8" does not match
    actual one "ISO8859_1"
    I don't use any extended characters in the xml document, and even if I
    did - I'm puzzled as to why DOM parser would process it without
    complaining but the SAX paser would not??
    Thanks,
    Marina

    Hello!
    I have run into a strange problem - I was parsing my xml document using
    WebLogic's DOM and SAX parsers to compare their performance, and I found
    that while using their DOM parser I was able to parse the document just
    fine, using the SAX parser gave me an error:
    org.xml.sax.SAXParseException: Declared encoding "UTF-8" does not match
    actual one "ISO8859_1"
    I don't use any extended characters in the xml document, and even if I
    did - I'm puzzled as to why DOM parser would process it without
    complaining but the SAX paser would not??
    Thanks,
    Marina

  • Can I Use  SAX  and DOM in same application?

    Hi,
    Here is my requirement. I am using Apache's SAX parser for my appliaction. The below xml is the sample xml i am processing.How can I know the parent of element 'C' using sax? Is there any way i can differentiate the element under <B> and under <A>? Or do i need to use DOM, when you encouter <C> and find out it's parent.?
    If any one had this situation,Please respond with some code examples.
    <A>
    <B>
    <C>aa</C>
    </B>
    <C>bb</C>
    </A>
    Thanx

    When using SAX, your application is notified about nodes as the parser encounters them, sequentially. There is no concept of a parent/child relationship for elements. When using SAX, you must develop this logic yourself.
    When using DOM, the contents of the XML file are read into a tree structure. If you have a reference to the node object that represents "C", you can get a reference to its parent by calling getParentNode().
    Hope this helps.
    Greg
    >
    Hi,
    Here is my requirement. I am using Apache's SAX parser
    for my appliaction. The below xml is the sample xml i
    am processing.How can I know the parent of element 'C'
    using sax? Is there any way i can differentiate the
    element under <B> and under <A>? Or do i need to
    use DOM, when you encouter <C> and find out it's
    parent.?
    If any one had this situation,Please respond with
    some code examples.
    <A>
    <B>
    <C>aa</C>
    </B>
    <C>bb</C>
    </A>
    Thanx

  • "Right" way to mix SAX and DOM in app

    I'm writing a standalone desktop application that reads and stores data as XML files. Eventually, it might be converted to use some web services, but it's not a priority right now.
    What I need to do is to use an XML file, which could be large, as kind of a database -- there are many entries, each with a unique identifier, and the application will query the file to find those it needs to match and returns those as objects I'm mapping.
    At the moment, because this is a personal project and I'm using it to learn more technologies, I'm trying to (somewhat artificially) restrict myself to the pure Sun APIs. So far, my investigations have pointed to JAXP (by including Java EE 5 libraries) with StAX (including JAX-WS). Which raises two questions:
    1) To do this "right", do I really need to bundle my app with the entire JaveEE+Metro stack?
    2) Is there a better solution than StAX that's fully Java 5 compliant, even if it means stepping out of the Sun box? I haven't found many references to other solutions that are more recent than 2004. Is parsing XML on an app that has nothing to do with an appserver that uncommon?

    More likely than not, I won't be abstracting to that degree. If the current structure isn't right, I'd update the app instead of storing that kind of information in more files.
    I imagine at this point an example would be more effective. The app is itself more of an inventory browser that can jump around different searches dynamically. As an illustration, imagine that it's an inventory for DVDs. One central file will be your collection (with each entry containing a movie ID, date of purchase, etc). Another file would be more static, a list of DVDs themselves. These entries would contain information about the package itself -- how many discs? What's the title of the package? Which special features does it have? It would also point to an entry in yet another file which would have information about the film, containing biographies of the people listed under the movie credits.
    Basically, I want a flatfile database that I can do joins on that are split up into different files. There are few files (here, one) that will be constantly updated by the user. The others could be modified if needed, but it's not going to be optimized for it. (For example, you could own a DVD that nobody's ever heard of, and put in the info yourself.) Periodically, one or more of the more static files is updated and will be downloaded into the app.
    One of the advantages I see for this is that, in the future, I could with few changes turn this into more of a web service. Instead of pushing changes in those few files, the app would look to a web service for the data it would now find in files on the user's hard drive. But for now, it also has to be one standalone package.
    To answer the question, the file that will most commonly be updated by the user is the one that I don't have problems loading into memory in full. It's the other data that it links to which I want to be able to search and load into objects dynamically. My current implementation is to run the file through SAX and grab the data as it sees it, but it's really ugly. That could very well be how I'm using it and not because I'm trying to shoehorn some functionality into a technique it doesn't fit, but I'd like to find that out. ;)

  • How to read from xml documents using either sax or dom

    dear all
    i have an xml document and i want to create a procedure to read the data from it .
    is it possible or not? thanks in advance

    Where is the xml document located?
    load it into an XMLTYPE and apply xpath as required.
    SQL> declare
      2    xml xmltype ;
      3  begin
      4    xml := xmltype('<node><value>this is text</value></node>') ;
      5    dbms_output.put_line('value='||xml.extract('/node/value/text()').getStringVal()) ;
      6  end ;
      7  /
    value=this is text
    PL/SQL procedure successfully completed.
    SQL>

  • DOM or SAX in parsing.Please GUIDE

    If I have a huge document that I need to parse,
    which is the recommended parser I shud use?
    SAX or DOM.??
    Isnt DOM impractical to use if the document fills
    gigabytes?
    Can some one please answer?

    SAX reads through the document and sends you events of what is read.
    SAX will never store any of the data which is read.
    You can do whatever you like with these events.
    DOM reads through the document and makes a memroy-model of the XML-file.
    This will store the complete XML-document in memory.
    Afterwards, you can browse this structered (in memory) data within your program.
    There is a tradeof between DOM and SAX.
    SAX has a smaller (constant) memory-usage (certainly with large XML-files),
    and has a faster processing of the data.
    But SAX delivers no real structured data and is only read in one direction.
    SAX should be used when large files are read only once in a linear direction
    and if the processing of the data does not rely to much on the structure of the data.
    DOM has a larger memory-usage (certainly with large XML-files),
    and has a slower processing of the data (due to the initial parsing of the file).
    But DOM delivers real structured data which can be manipulated on the structure in any direction.
    DOM should be used when smaller files are read, when many different processing-steps should
    be performed on the data or when the processing of the data is heavily relying on the structure of the data.
    remark that DOM is using SAX as the underlying mechanism for reading the data.
    DOM will read the data using SAX and will build the in-memory structured model of the data
    according to the SAX-events.
    remark also that other solutions such as JDOM and minimal DOM's exist.
    kind regards,

  • XML: SAX vs. DOM vs. jDom etc.

    Is there any reason to use SAX or DOM? I mean is jdom always the best choice? Some information?

    http://java.sun.com/webservices/docs/1.0/tutorial/doc/JAXPDOM2.html#67733

  • Possible to overwrite parse method in DOM with SAX Handler?

    Hi,
    Is it possible to overwrite the parse method within DOM?
    What a want to do is:
         private Node parseXml( String text ) throws SAXParseException, SAXException, IOException, ParserConfigurationException
              SAXParserFactory factory = SAXParserFactory.newInstance();
                   factory.setNamespaceAware(true);
                   SAXParser parser = factory.newSAXParser();
                   TestHandler handler = new TestHandler();
                   // Parse the file
                   parser.parse(new InputSource(new StringReader(text)), handler);
              return (Node)handler.getRoot();
         } //end parseXml()The reason I want to use this is that within SAX I can write my own line counter so I keep a count of which node is on which line of text.
    What I was thinking is that I could use SAX to return DOM Nodes with the line number attached, possibly using the node.setUserData() method?!
    I have tried to play around with it and it doen't seem to work, can anyone help?
    Cheers Alex

    I have managaed to re-write my SAX parser to create a JTree, this works perfectly I can move through the tree and each line of text that corresponds to the node is highlighted.
    My problem is however that in my application I have used the DOM structure throughout so some of the functionality is lost.
    Is I understand that JAXP uses both SAX and DOM together, so I was wondering if it is possible to combine my sax parse method within the DOM?
    If anything is unclear please say and I will try and explain better. The main reason for doing this is that I want to keep a reference to which line of text each node of the dom tree represents. I have not been able to implemnet one in DOM however using SAX I have managed.
    Many thanks,
    Alex

  • Choosing between SAX & DOM & StringBuffer class for writing XML

    Hii Javaties
    I need to create a XML document from a swing application.
    I need to store this xml in a file.
    For writing XML which parser should i use SAX or DOM or should i go for StringBuffer class.

    I'm not aware of a way to use SAX to create an XML document. It is used to view the contents of an existing XML document or data that is well-structured.
    That leaves you with a choice of DOM or StringBuffer. We cannot tell enought about what you are doing to know which is better. Each has some advantages and disadvantages.
    StringBuffer:
    + less overhead
    - no assistance in ensuring the validity of the result
    + greater control over layout of resulting document (how and when to indent)
    - any control over layout must be done by you.
    DOM (although this task is easier in JDOM)
    - more overhead
    - some learning curve
    + result is guaranteed to be valid XML.
    + there are pretty formatters to generate good looking results
    - there may be layout choices that the standard pretty formatters cannot do
    As an example, consider this part of the Document being created.
    You want <name>John Doe</name>
    With a StringBuffer, you can just add these pieces. With DOM, you need to:
    1) create an Element named "name".
    2) create a Text object with a value of "John Doe".
    3) add the Text object as a child of the Element
    4) add the Element to the parent
    Having done the DOM version (also a JDOM version) several times, I have developed an XMLHelper class that has several static methods to make simple, common constructs.
    It lets me do things like:
    parent.addChild( XmlHelper.makeElement( "name", customerName ) );
    So, the answer to your question is the same as many other questions: It depends.
    Dave Patterson

  • Guide Lines to Use SAX,DOM using javax.xml. * files only, I

    Hi Respected Recievers,
    My Name is Surya, i am working as a Java
    Programmer.
    I need JAR files to run SAX and DOM properly.
    I don't want to Use xerces.jar or jaxp.jar files, i will use only javax.xml.* files only.
    Could provide some examples on those topics like reading,modifying,the files using SAX and DOM.
    Thanks&Regards
    Surya

    Add rt.jar to Classpath.
    DOM Tutorial
    http://java.sun.com/webservices/docs/1.0/tutorial/doc/JAXPDOM3.html
    SAX tutorial
    http://java.sun.com/j2ee/1.4/docs/tutorial-update2/doc/JAXPSAX.html

  • Performance question

    Which mapping is performance wise better, comparison between graphical, abap, java and XSLT mapping.
    suppose when the mapping is simple which is better and when mapping is complex which is better.

    Hi Srinu,
    I thought i will start of from scratch.Mapping is basically done to convert one form of xml into another form. This can be done using either of them mentioned below.
    - Graphical mapping
    - XSLT mapping
    - JAVA mapping
    - ABAP mapping
    There is no hard and fast rule for using the mapping techniques. But, I will try to put things in the right perspective for you.
    Graphical Mapping is used for simple mapping cases. When, the logic for your mapping is simple and straight forward and it does not involve any complex logic.
    Java and XSLT mapping are used when graphical mapping cannot help you.
    When the choice is between Java And XSLT, XSLT is simpler than java mapping and easier. But, it has its drawbacks. One among them being that you cannot use Java APIs and Classes in it. There might be cases in your mapping when you will have to perfrom something like a properties file look up or a DB lookup, such scenarios are not possible in XSLT and so, when you want to use some specific Java API's you will have to go for Java Mapping.
    Java Mapping uses 2 types of parsers. DOM and SAX. DOM is easier to use with lots of classes to help you create nodes and elements, but , DOM is very processor intensive.
    SAX parser is something that parses your XML one after the other, and so is not processor intensive. But, it is not exaclty easy to develop either.
    To know more about each of them please go thru the following links. And if you ask me your which is better, it depends basically on the scenario you implementing and the complexity involved. Anyways please go thru the following links:
    Graphical mapping
    http://help.sap.com/saphelp_nw04/helpdata/en/6d/aadd3e6ecb1f39e10000000a114084/content.htm
    http://help.sap.com/saphelp_nw04/helpdata/en/43/c4cdfc334824478090739c04c4a249/content.htm
    /people/bhanu.thirumala/blog/2006/02/02/graphical-message-mapping-150-text-preview
    http://www.sapgenie.com/netweaver/xi/mapping1.htm
    /people/alessandro.guarneri/blog/2006/01/26/throwing-smart-exceptions-in-xi-graphical-mapping
    XSLT mapping
    http://help.sap.com/saphelp_nw04/helpdata/en/73/f61eea1741453eb8f794e150067930/content.htm
    http://www.w3.org/TR/xslt20/
    JAVA mapping
    http://help.sap.com/saphelp_nw04/helpdata/en/e2/e13fcd80fe47768df001a558ed10b6/content.htm
    http://java.sun.com/j2se/1.4.2/docs/api/org/w3c/dom/package-frame.html
    ABAP mapping
    /people/r.eijpe/blog
    To know more about the value mapping tools for the SAP Exchange Infrastructure (XI), please go thru the following link:
    http://www.applicon.dk/fileadmin/filer/XI_Tools/ValueMappingTool.pdf
    To get an idea as to what value mapping is, please go thru the following links:
    http://help.sap.com/saphelp_nw04/helpdata/en/13/ba20dd7beb14438bc7b04b5b6ca300/frameset.htm
    http://help.sap.com/saphelp_nw04/helpdata/en/f2/dfae3d47afd652e10000000a114084/frameset.htm
    http://help.sap.com/saphelp_nw04/helpdata/en/2a/9d2891cc976549a9ad9f81e9b8db25/content.htm
    most of the links that I have provided also helps you get the step by step procedure of doing the same. And also involves the procedure to implement certain advanced features.
    And please go through this link which clearly explains the types of mappings.
    /people/ravikumar.allampallam/blog/2005/02/10/different-types-of-mapping-in-xi
    Hope this clears your doubt fully.
    Regards,
    Abhy

  • Bad performance on Factory.parse method

    Hi all,
    I have some classes created from an XML Schema.
    I'm using them to load an XML from disk instead of using SAX or DOM.
    The problem is that I have monitorized the performance with OptimizeIt
    and every time I call the XXXXType.Factory.Parse(InputStream file)
    method the number of instances in memory of the following classes:
    com.bea.xbean.store.Splay$[Annotation, Attr, Begin, Comment, CopyContext, CursorGobber]
    enlarges as much as the XML file in disk becomes bigger. It's a big problem for
    an application that continously loads files about 1.5 MB. I force OptimizeIt to
    call the Garbage Collector, but those objects are never removed.
    Does anybody have noticed the same issue?
    Thank you very much,
    Gustavo.

    Hi all,
    I have some classes created from an XML Schema.
    I'm using them to load an XML from disk instead of using SAX or DOM.
    The problem is that I have monitorized the performance with OptimizeIt
    and every time I call the XXXXType.Factory.Parse(InputStream file)
    method the number of instances in memory of the following classes:
    com.bea.xbean.store.Splay$[Annotation, Attr, Begin, Comment, CopyContext, CursorGobber]
    enlarges as much as the XML file in disk becomes bigger. It's a big problem for
    an application that continously loads files about 1.5 MB. I force OptimizeIt to
    call the Garbage Collector, but those objects are never removed.
    Does anybody have noticed the same issue?
    Thank you very much,
    Gustavo.

Maybe you are looking for

  • How can I create a new own class in WebDynpro?

    I test the Web Dynpro for a while and found all the java classes were generated automatically. I've tried to create an own class. But after I rebuild the project, all of my own classes were cleared.

  • Video Stutters in Color

    All, My video playback is very stuttery in Color. Obviously I can still work fine with my color correction, but I was just wondering if this is normal. Thanks, Trey

  • Cheep black and white printer for iPads?

    What is a cheep printer that my students can print directly from their iPads? I only want them to be able to print black and white.

  • How many ARD licenses do I need?

    I have a simple objective: I want to use ARD to control my mother's computer remotely.  Obviously, I need a $79.99 license for my computer.  Do I need a second license for my mother's computer?

  • Queue Blocking and File picked Partially Problem ?

    Hi All, The File which is placed in FTP server having more 50000 to 65000 rows . when XI picks the File the Partial Data is processed. In SXMB_MONI if we check the message we are able to see some rows not full Data which was in file. it's also blocki