我有生成xml的XML流代码,如下所示。代码和生成的xml如下所示。现在,我想从AEM DAM读取另一个XML文件(如下所示),并附加到使用XMLStreamWriter流生成的xml。
XML Stream代码如下所示
XMLOutputFactory of = XMLOutputFactory.newFactory();
XMLStreamWriter writer of.createXMLStreamWriter(slingResponse.getWriter());
stream.writeStartDocument("1.0");
stream.writeStartElement("", “list", “http://www.google.com/schemas/list/1.1");
stream.writeNamespace("", “http://www.google.com/schemas/list/1.1”);
writer.writeStartElement(NS, “books”);
writer.writeStartElement(“http://www.google.com/schemas/list/1.1”, “name”)
writer.writeCharacters(“test1”);
writer.writeEndElement();
writer.writeStartElement(“http://www.google.com/schemas/list/1.1”, “author”)
writer.writeCharacters(“sample1”);
writer.writeEndElement();
writer.writeEndDocument();
上面的代码正在生成像这样的xml文件 -
<list xmlns="http://www.google.com/schemas/list/1.1">
<books>
<name>test1</loc>
<author>sample1</author>
</books>
<books>
<name>test2</loc>
<author>Sample2</author>
</books>
<books>
<name>test3</loc>
<author> Sample3</author>
</books>
</list>
现在,下面是存储在AEM DAM中的XML内容。在这里,我想从AEM DAM读取这个XML文件,并使用XMLStreamWriter流附加到生成的xml(上面)
<list xmlns="http://www.google.com/schemas/list/1.1">
<books>
<name>test4</loc>
<author>sample4</author>
</books>
<books>
<name>test5</loc>
<author>Sample5</author>
</books>
<books>
<name>test6</loc>
<author> Sample6</author>
</books>
</list>
目前,我有以下代码段来访问AEM DAM中的文件。但是如何读取这个xml文件并写入XMl流?
Map<String, String> map = new HashMap<String, String>();
map.put("type", "dam:Asset");
map.put("path", "/content/dam/sample/en");
map.put ("property", "jcr:content/metadata/dc:format");
map.put ("property.value", "application/xml");
builder= resourceResolver.adaptTo(QueryBuilder.class);
Query query = builder.createQuery(PredicateGroup.create(map), session);
SearchResult sr= query.getResult();
最后,我想要这样的输出 -
<list xmlns="http://www.google.com/schemas/list/1.1">
<books>
<name>test1</loc>
<author>sample1</author>
</books>
<books>
<name>test2</loc>
<author>Sample2</author>
</books>
<books>
<name>test3</loc>
<author> Sample3</author>
</books>
<books>
<name>test4</loc>
<author>sample4</author>
</books>
<books>
<name>test5</loc>
<author>Sample5</author>
</books>
<books>
<name>test6</loc>
<author> Sample6</author>
</books>
</list>
答案 0 :(得分:1)
您可以使用org.apache.jackrabbit.commons.JcrUtils#readFile
InputStream
获取
Node node = ... // your xmlNode
InputStream in = JcrUtils.readFile(node);
从那里你可以使用输入流来做任何你喜欢的事情我不确定你想要做什么。您可以使用IOUtils.toString(in)
将文件作为字符串获取,也可以将InputStream
转换(复制)到OutputStream
,并将其与您的流编写器一起使用,如:
OutputStream out = ... // this is your output stream
IOUtils.copy(in,out);
XMLOutputFactory outputFactory = XMLOutputFactory.newFactory();
XMLStreamWriter writer = outputFactory.createXMLStreamWriter(out);
此处提及的
IOUtils
就是这样的一个:org.apache.commons.io.IOUtils
默认情况下会附带AEM,或者您可以在您的pom中添加不同的版本https://commons.apache.org/proper/commons-io/
答案 1 :(得分:1)
如果您将xml存储为dam:Asset
,则jcr:content/renditions/original
节点(在资产节点下)将包含原始二进制数据,您应该从中读取 -
添加到您的代码:
Node node = sr.getNodes().next() //first node
InputStream is = node.get("jcr:content/renditions/original/jcr:content").getProperty("jcr:data").getBinary().getStream();
//now you can work with this stream (is) and its content
// if you want to simply write to slingresponse
slingResponse.getWriter().write(IOUtils.toString(is, UTF8));
// if you want to work with xml elements you will have to parse the xml first