XML Namespaces

XmlField supports XML namespaces. You can easily create objects mixing data from different namespaces, or you can use one object per namespace.

Starting from the following XML file :

<a:entry xmlns:a="http://www.w3.org/2005/Atom" xmlns="http://www.w3.org/1999/xhtml" >
    <a:title type="xhtml">
        <div>
            <span class="name">CD Catalog</span>
            <span class="format">Atom</span>
        </div>
    </a:title>
    <a:id>12345</a:id>
    <a:updated>2003-12-14T18:30:02Z</a:updated>
    <a:author>
        <a:name>Author</a:name>
    </a:author>
    <a:content type="xhtml">
        <div>
            <div class="cd">
                <span class="title">01</span>
                <span class="price">999999</span>
                <span class="artist">QL0001</span>
            </div>
        </div>
    </a:content>
</a:entry>    
        

We just have to add the namespaces definitions and start using namespace prefixes in FieldXPath values :

        
AtomCatalog.java :
        
@Namespaces({ "xmlns:a=http://www.w3.org/2005/Atom", "xmlns:x=http://www.w3.org/1999/xhtml" })
@ResourceXPath("/a:entry")
public interface AtomCatalog {

	@FieldXPath("a:title/x:div/x:span[@class='name']")
	String getName();

	@FieldXPath("a:content/x:div/x:div[@class='cd']")
	AtomCd[] getCd();

	AtomCd addToCd();

}


AtomCd.java :

@Namespaces({ "xmlns:a=http://www.w3.org/2005/Atom", "xmlns:x=http://www.w3.org/1999/xhtml" })
@ResourceXPath("x:div[@class='cd']")
public interface AtomCd {
    @FieldXPath("x:span[@class='title']")
    String getTitle();

    void setTitle(String t);

    @FieldXPath("x:span[@class='price']")
    float getPrice();

    void setPrice(float price);
}

    

Then simply use XmlField as you would normally do :

String xmlContent="<a:entry xmlns:a="http://www.w3.org/2005/Atom" xmlns="http://www.w3.org/1999/xhtml" >(...)</a:entity>";
XmlField xf = new XmlField();
AtomCatalog catalog = xf.xmlToObject( xmlContent, AtomCatalog.class );

System.out.println (catalog.getName());

AtomCd cd = db.addToCd();
cd.setTitle("My new cd");


String result = xf.objectToXml(catalog);

    
comments powered by Disqus