abdera-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Takasho <taka...@gmail.com>
Subject Using xpath with Namespaces
Date Thu, 27 Sep 2007 11:41:20 GMT
Hi All,

I'm parsing a local xml file with extensions which has a couple of entries
in it. One of the entries contains extensions, The local xml file is built
by Abdera and it looks like this:
<feed xmlns="http://www.w3.org/2005/Atom">
    <id>http://example.org/media/audio</id>
    <updated>2007-09-27T09:33:11.852Z</updated>
    <title type="text">This is a feed</title>
    <link href="http://example.org/media/audio" />
    <link href="http://example.org/media/audio" rel="self" />
    <entry>
        <id>http://example.org/media/audio/xmlEntry</id>
        <updated>2007-09-27T09:33:12.446Z</updated>
        <title type="text">This is an entry with xml</title>
        <content type="application/xml">
            <a xmlns="">
                <b>
                    <text>My Text</text>
                </b>
            </a>
        </content>
        <author>
            <name>Takasho</name>
        </author>
        <link href="http://example.org/media/audio/xmlEntry" />
    </entry>
    <entry xmlns:audio="http://example.org/namespaces/media/audio/"
        xmlns:media="http://example.org/namespaces/media/">
        <id>http://example.org/media/audio/extensionsEntry</id>
        <updated>2007-09-27T09:33:12.446Z</updated>
        <title type="text">This is an entry with text/html and
Extensions</title>
        <content type="text/html">
            &lt;html>&lt;head/>&lt;body>&lt;p>This is a full HTML
            document&lt;/p>&lt;/body>&lt;/html>
        </content>
        <author>
            <name>Adam</name>
        </author>
        <link href="http://example.org/media/audio/extensionsEntry" />
        <audio:artist>Magic Man and the Three Tweezers</audio:artist>
        <audio:artist>Billy Holiday</audio:artist>
        <audio:artist>The Crazy World of Arthur Brown</audio:artist>
        <audio:label>EMPIRE Records</audio:label>
        <media:device>N73</media:device>
        <media:device>N95</media:device>
        <media:device>N60</media:device>
        <media:format>3gp</media:format>
    </entry>
</feed>

I'm using the following to parse the xml file:

        Document<Feed> doc = abdera.getParser().parse(in);
        // Grab the root feed
        Feed feed = doc.getRoot();

        XPath xpath = Abdera.getNewXPath();

        HashMap<String, String> ns = new HashMap<String, String>();
        ns.put("audio", "http://example.org/namespaces/media/audio/");
        ns.put("media", "http://example.org/namespaces/media/");

        // Determine if a feed contains a specific extension
        for (Entry entry : feed.getEntries()) {
            System.out.println(entry.getTitle());
            List theAudio = xpath.selectNodes("//audio:*", entry, ns);
            List theMedia = xpath.selectNodes("//media:*", entry, ns);
            System.out.println("Has Audio Extension: " + theAudio.toString
());
            System.out.println("Has Media Extension: " + theMedia.toString
());
        }
The output is:

This is an entry with xml
Has Audio Extension: [<audio:artist xmlns:audio="
http://orange.co.uk/namespaces/media/audio/">Magic Man and the Three
Tweezers</audio:artist>, <audio:artist xmlns:audio="
http://orange.co.uk/namespaces/media/audio/">Billy Holiday</audio:artist>,
<audio:artist xmlns:audio="http://orange.co.uk/namespaces/media/audio/">The
Crazy World of Arthur Brown</audio:artist>, <audio:label xmlns:audio="
http://orange.co.uk/namespaces/media/audio/">EMPIRE Records</audio:label>]
Has Media Extension: [<media:device xmlns:media="
http://orange.co.uk/namespaces/media/">N73</media:device>, <media:device
xmlns:media="http://orange.co.uk/namespaces/media/">N95</media:device>,
<media:device xmlns:media="http://orange.co.uk/namespaces/media/">N60</media:device>,
<media:format xmlns:media="http://orange.co.uk/namespaces/media/
">3gp</media:format>]
This is an entry with text/html and Extensions
Has Audio Extension: [<audio:artist xmlns:audio="
http://orange.co.uk/namespaces/media/audio/">Magic Man and the Three
Tweezers</audio:artist>, <audio:artist xmlns:audio="
http://orange.co.uk/namespaces/media/audio/">Billy Holiday</audio:artist>,
<audio:artist xmlns:audio="http://orange.co.uk/namespaces/media/audio/">The
Crazy World of Arthur Brown</audio:artist>, <audio:label xmlns:audio="
http://orange.co.uk/namespaces/media/audio/">EMPIRE Records</audio:label>]
Has Media Extension: [<media:device xmlns:media="
http://orange.co.uk/namespaces/media/">N73</media:device>, <media:device
xmlns:media="http://orange.co.uk/namespaces/media/">N95</media:device>,
<media:device xmlns:media="http://orange.co.uk/namespaces/media/">N60</media:device>,
<media:format xmlns:media="http://orange.co.uk/namespaces/media/
">3gp</media:format>]

The problem seems to be that the xpath functions are not respecting the base
element. The output is the same if I use 'doc', 'feed' or 'entry' as the
base element in the xpath query.

I am wanting to detect on a per entry basis whether the entry has extensions
and what the extensions are. I may however have got this completely wrong
but from the Javadocs this was my  understanding of it.

Thanks,

Takasho

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message