What is the difference between localname and qname?

The qualified name includes both the namespace prefix and the local name: att1 and foo:att2. Sample XML <root xmlns=”http://www.example.com/DEFAULT” att1=”Hello” xmlns:foo=”http://www.example.com/FOO” foo:att2=”World”/> Java Code: att1 Attributes without a namespace prefix do not pick up the default namespace. This means while the namespace for the root element is “http://www.example.com/DEFAULT”, the namespace for the att1 attribute is … Read more

How to set Saxon as the Xslt processor in Java?

There are multiple ways to do this (in order of lookup precedence): Direct Instantiation Explicitly instantiate the Saxon factory (with a nod to Michael’s comment above): TransformerFactory fact = new net.sf.saxon.TransformerFactoryImpl() This approach means that your code is locked into using Saxon at compile time. This can be seen as an advantage (no risk of … Read more

How to parse XML using the SAX parser

So you want to build a XML parser to parse a RSS feed like this one. <rss version=”0.92″> <channel> <title>MyTitle</title> <link>http://myurl.com</link> <description>MyDescription</description> <lastBuildDate>SomeDate</lastBuildDate> <docs>http://someurl.com</docs> <language>SomeLanguage</language> <item> <title>TitleOne</title> <description><![CDATA[Some text.]]></description> <link>http://linktoarticle.com</link> </item> <item> <title>TitleTwo</title> <description><![CDATA[Some other text.]]></description> <link>http://linktoanotherarticle.com</link> </item> </channel> </rss> Now you have two SAX implementations you can work with. Either you use the org.xml.sax … Read more

Is there any XPath processor for SAX model?

My current list (compiled from web search results and the other answers) is: http://code.google.com/p/xpath4sax/ http://spex.sourceforge.net/ https://github.com/santhosh-tekuri/jlibs/wiki/XMLDog (also contains a performance chart) http://www.cs.umd.edu/projects/xsq/ (uniersity project, dead since 10 years, GPL) MIT-Licensed approach http://softwareengineeringcorner.blogspot.com/2012/01/conveniently-processing-large-xml-files.html Other parsers/memory models supporting fast XPath: http://vtd-xml.sourceforge.net/ (“The world’s fastest XPath 1.0 implementation.”) http://jaxen.codehaus.org/ (contains http://www.saxpath.org/) http://www.saxonica.com/documentation/sourcedocs/streaming/streamable-xpath.html The next step is to use … Read more

XML parsing – ElementTree vs SAX and DOM

ElementTree is much easier to use, because it represents an XML tree (basically) as a structure of lists, and attributes are represented as dictionaries. ElementTree needs much less memory for XML trees than DOM (and thus is faster), and the parsing overhead via iterparse is comparable to SAX. Additionally, iterparse returns partial structures, and you … Read more

When should I choose SAX over StAX?

OverviewXML documents are hierarchical documents, where the same element names and namespaces might occur in several places, having different meaning, and in infinitive depth (recursive). As normal, the solution to big problems, is to divide them into small problems. In the context of XML parsing, this means parsing specific parts of XML in methods specific … Read more