【发布时间】:2013-01-11 05:48:07
【问题描述】:
虽然描述看起来很长,但实际上很简单。
我需要什么
我有以下 xml 文档
<?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>
请注意,xml 基本上包含三个命名空间
- 原子:http://www.w3.org/2005/Atom
- dcterms:http://purl.org/dc/terms/
- 开放搜索:http://a9.com/-/spec/opensearch/1.1/
现在我需要生成另一个单个 atom:entry 节点的 xml 文档。如下所示
<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>
我得到了什么
我正在使用XOM,到目前为止我尝试过
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());
而且我只使用一个命名空间声明来跟踪 xml。如下所示。
<?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>
问题
它只包含 Atom 命名空间 :( 因此它不是一个有效的 XML。因为它有一个子节点 dcterms:identifier 缺少命名空间。
我非常需要帮助才能摆脱这个问题。期待任何帮助。
【问题讨论】:
标签: java xml xml-namespaces xom