My favorites | Sign in
Project Home Downloads Wiki Issues Source
READ-ONLY: This project has been archived. For more information see this post.
Search
for
XmlUsage  
How to use the XML parser.
Updated Aug 19, 2011 by harald.pehl

Prerequisite

To use the XML parser make sure you have the JAR file on the classpath and include the following code snippet in your module definition:

<inherits name="name.pehl.totoe.xml.XML" />

Usage

Like the GWT XML parser you start with an XML input as string. Once you have a document instance you can call methods to get the root element, find nodes by type or execute XPath queries:

String xml = "...";
Document document = new XmlParser().parse(xml, "xmlns:acme=\"http://www.acme.org\"");
Element root = document.getRoot();
List<Comment> comments = document.findByType(NodeType.COMMENT);
List<Node> products = document.selectNodes("//acme:products");

For a complete overview over the features feel free to take a look at the API documentation.

Namespaces

Totoe supports namespaces both in the XML document and in XPath queries. To use namespaces call one of the following parse methods in XmlParser:

  • XmlParser.parse(String xml, String namespaces): The namespaces are given as a whilespace-seperated list of namespace declarations as those would appear in an XML document.
  • XmlParser.parse(String xml, Map<String,String> namespaces): The namespaces are given as a map with the namespace prefix as key and the namespace uri as value.

Default namespace

If you want to parse a XML document with a default namespace and you want to reference the default namespace later on in XPath expressions, you have to provide a prefix for the default namespace. Suppose you have the following XML document:

<?xml version="1.0" encoding="UTF-8"?>

<swissArmyKnife xmlns="http://code.google.com/p/totoe" xmlns:foo="http://code.google.com/p/totoe/foo"
    xmlns:bar="http://code.google.com/p/totoe/bar" id="B001DZTJRQ">
    <!-- 
        XML that has erything inside. Just like the Wenger 16999 Swiss Army Knife 
        http://www.amazon.com/Wenger-16999-Swiss-Army-Knife/dp/B001DZTJRQ/ 
    -->
    <description><![CDATA[
        Call it what you will, it's a knife that's unrivaled, impractical, 
        and enormous, it's more knife than one can carry or would fit in one's pocket
    ]]></description>
    <!-- long live the metric system! -->
    <foo:dimensions foo:unit="cm">11.8 x 10.8 x 4.3</foo:dimensions>
    <bar:weight bar:unit="g">700</bar:weight>
    <functions number="more than you'll ever need">
        <rocketLauncher kind="advanced" foo:range="intercontinental" bar:dangerous="indeed">5</rocketLauncher>
        <calculator eval="1 &lt; 2">2 &gt; 1</calculator>
        <bttf:fluxCapacitor xmlns:bttf="http://en.wikipedia.org/wiki/Back_to_the_Future">
            <bttf:power unit="gigawatts">1.21</bttf:power>
        </bttf:fluxCapacitor>
    </functions>
</swissArmyKnife>

Use the following code to parse the XML:

String xml = ...;
String namespaces = "xmlns:default=\"http://code.google.com/p/totoe\" "
    + "xmlns:foo=\"http://code.google.com/p/totoe/foo\" "
    + "xmlns:bar=\"http://code.google.com/p/totoe/bar\" "
    + "xmlns:bttf=\"http://en.wikipedia.org/wiki/Back_to_the_Future\"";
Document document = new XmlParser().parse(xml, namespaces);

To select the <functions> element use the following XPath expression

Node functions = document.selectNode("//default:functions");

To select the unit attribute of the <bttf:power> element use this expression

String unit = document.selectValue("//bttf:power/@unit");
Powered by Google Project Hosting