OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

 


 

   Re: [xml-dev] Does SAX make sense?

[ Lists Home | Date Index | Thread Index ]

Karl Waclawek wrote:

>In my personal experience that happens quite often.
>The app's object model is not always the same as the DOM.
>
>As a middle of the way approach it might be nice to have
>a PULL API where one has the choice - as one encounters
>the nodes of the document - to "keep" the ones that
>one would like to use later, IOW, building a DOM tree as
>a side effect of pull parsing.
>  
>
hi,

i completely agree and it is really trivial to do when you have pull 
parsing API available such as (XmlPull API) - i am doing it for quite 
long time and i am finding flexibility to build node tree from any place 
in stream very useful.

here is typical scenario: first you need to open XML stream:

FileInputStream fin = ...
XmlPullParser pp = builder.getFactory().newPullParser();
pp.setInput(fin);

later you can do processing of header content extracting items i care 
and skip whole subtrees to ignore header entries i do not care about:

        while(pp.nextTag()== XmlPullParser.START_TAG) {
            if(NS.equals(pp.getNamespace()) == false) {
                throw new MException("all header elements must be in 
namespace "+NS
                                         +" and not "+pp.getNamespace()
                                         +" for element 
'"+pp.getName()+"': position"
                                         +pp.getPositionDescription());
            }
            if("template".equals(pp.getName())) {
                header.template = pp.nextText();
            } else if("title".equals(pp.getName())) {
                header.title = pp.nextText();
            } else if("tagline".equals(pp.getName())) {
                header.tagline = pp.nextText();
            } else {
                builder.skipSubTree(pp); //call builder.parseFragment() 
to preserve XML fragment
            }
        }

however when processing entries i can take entry content (that is XHTML) 
and preserve it for later processing as XML tree (such as display):

      while(( eventType = pp.next()) != XmlPullParser.END_TAG) {
            if(eventType == XmlPullParser.START_TAG) {
                if(XHTML.equals(pp.getNamespace()) == false) {
                    throw new MException("all entry children must be in 
namespace "+XHTML+pp.getPositionDescription());
                }
                entryXhtmlContent.add( builder.parseFragment(pp) );
            } else if(eventType == XmlPullParser.TEXT) {
                entryXhtmlContent.add( pp.getText() );
            }
        }

this way i have most of benefits of DOM however i can build XML trees 
only for XML content i need to access in tree-like manner (for example 
to use XPATH later).

thanks,

alek

-- 
"Mr. Pauli, we in the back are all agreed that your theory is crazy. 
What divides us is whether it is crazy enough to be true." Niels H. D. Bohr






 

News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 2001 XML.org. This site is hosted by OASIS