user2581831
user2581831

Reputation: 117

AEM 6.2 - Read AEM DAM XML and append to another XML using XMLStreamWriter stream

I have XML stream code which is generating the xml like shown below. Code and generated xml is shown below. Now, I want to read another XML file from AEM DAM (content shown below) and append to the xml generated using XMLStreamWriter stream.

XML Stream code is shown below

 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();

Above code is generating the xml file like this -

     <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>

Now, below is the content of XML which is stored in AEM DAM. Here, I want to read this XML file from AEM DAM and append to the xml generated(above) using XMLStreamWriter stream

   <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>

Currently, I have below code snippet to access the file from AEM DAM. But how to read this xml file and write to XMl stream?

    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();

Finally, i want output like this -

     <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>

Upvotes: 0

Views: 600

Answers (2)

awd
awd

Reputation: 2322

If you are storing xml as dam:Asset, the jcr:content/renditions/original node (under the asset node) will contain the original binary data, and you should read from that-

adding to your code:

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

Upvotes: 1

Ahmed Musallam
Ahmed Musallam

Reputation: 9753

You can get a node as an InputStream using org.apache.jackrabbit.commons.JcrUtils#readFile

 Node node = ... // your xmlNode
 InputStream in = JcrUtils.readFile(node);

From there you can use the input stream to do whatever you like I am not exactly sure what you are trying to do. You can get the file as string by using IOUtils.toString(in) or You could convert (copy) the InputStream to an OutputStream and use it with your stream writer like:

OutputStream out = ... // this is your output stream
IOUtils.copy(in,out);
XMLOutputFactory outputFactory = XMLOutputFactory.newFactory();
XMLStreamWriter writer = outputFactory.createXMLStreamWriter(out);

The IOUtils mentioned here is this one: org.apache.commons.io.IOUtils which ships by default with AEM or you can include a different version in your pom https://commons.apache.org/proper/commons-io/

Upvotes: 1

Related Questions