Though the description looks like really long, its actually quite straightforward.
What I need
I have the following xml document
<?xml version="1.0" encoding="UTF-8"?>
<atom:feed xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:atom="http://www.w3.org/2005/Atom" type="application/atom+xml;type=feed">
<atom:link rel="self" href="http://hostname/thirdparty/playlist"/>
<atom:id>vuter id</atom:id>
<atom:title>Untitled</atom:title>
<opensearch:totalResults>249</opensearch:totalResults>
<opensearch:startIndex>1</opensearch:startIndex>
<opensearch:itemsPerPage>249</opensearch:itemsPerPage>
<atom:entry type="application/atom+xml;type=entry">
<atom:id>12345678</atom:id>
<atom:title>Playlist example 1</atom:title>
<atom:link rel="self" type="application/atom+xml;type=entry" href="http://hostname/thirdparty/playlist/2101211130000011141"/>
<dcterms:identifier>2101211130000011141</dcterms:identifier>
</atom:entry>
<atom:entry type="application/atom+xml;type=entry">
<atom:id>12345678</atom:id>
<atom:title>Playlist example 2</atom:title>
<atom:link rel="self" type="application/atom+xml;type=entry" href="http://hostname/thirdparty/playlist/2101211090000000341"/>
<dcterms:identifier>2101211090000000341</dcterms:identifier>
</atom:entry>
</atom:feed>
Notice that the xml is containing basically three namespaces
- Atom: http://www.w3.org/2005/Atom
- dcterms: http://purl.org/dc/terms/
- opensearch: http://a9.com/-/spec/opensearch/1.1/
Now I need to generate another xml document of a single atom:entry node. Like below
<atom:entry type="application/atom+xml;type=entry" xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:atom="http://www.w3.org/2005/Atom">
<atom:id>12345678</atom:id>
<atom:title>Playlist example 1</atom:title>
<atom:link rel="self" type="application/atom+xml;type=entry" href="http://hostname/thirdparty/playlist/2101211130000011141"/>
<dcterms:identifier>2101211130000011141</dcterms:identifier>
</atom:entry>
What Im getting
I am using XOM and so far ive tried with
Element rootElem = new Builder().build(ins).getRootElement();
xc = XPathContext.makeNamespaceContext(rootElem);
Element firstEntry = (Element) rootElem.query("atom:entry", xc).get(0);
Document doc = new Document((Element)firstEntry.copy());
System.out.println(doc.toXML());
And Im getting following xml with only one namespace declaration. Like below.
<?xml version="1.0"?>
<atom:entry xmlns:atom="http://www.w3.org/2005/Atom" type="application/atom+xml;type=entry">
<atom:id>12345678</atom:id>
<atom:title>Playlist example 1</atom:title>
<atom:link rel="self" type="application/atom+xml;type=entry" href="http://hostname/thirdparty/playlist/2101211130000011141" />
<dcterms:identifier xmlns:dcterms="http://purl.org/dc/terms/">2101211130000011141</dcterms:identifier>
</atom:entry>
Problem
Its containning only Atom namespace :( Hence its not a valid XML. Because it has a child node dcterms:identifier whose namespace is missing.
I need help badly to get out of this problem. Looking forward for any help.