简体   繁体   中英

Reading large xml files one node at a time in Java with a pull parser?

I'd like to parse large XML files and read in a complete node at a time from Java. The files are to large to put in a tree. I'd like to use a pull parser if possible since it appears to be easier to program for. Given the following XML data
Instead of having to check every event while using the StAX parser I'd like each call to hasNext or some similar function to return an object containing the complete info on a record node. When using Perl XML::LibXML::Reader allows me to do this using it's read method so I'm looking for an equivalent in Java.

Commons Digester is really good for this type of problem. It allows you to configure parsing rules whereby when the parser encounters certain tags it performs some action (eg calls a factory method to create an object). You don't have to write any parsing code, making development fast and lightweight.

Below is a simple example pattern you could use:

<pattern value="myConfigFile/foos/foo">
    <factory-create-rule classname="FooFactory"/>
    <set-next-rule methodname="processFoo" paramtype="com.foo.Foo"/>
</pattern>

When the parser encounters the "foo" tag it will call createObject(Attributes) on FooFactory , which will create a Foo object. The parser will then call processFoo on the object at the top of the Digester stack (you would typically push this onto the stack before commencing parsing). You could therefore implement processFoo to either add these objects to a collection, or if your file is too big simply process each object as it arrives and then throw it away.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM