Search code examples
javaxmlxstream

Need Help in creating xml with list of nodes with different node name


I am using xstream and want to generate xml like this.

<relatedImages jcr:primaryType="nt:unstructured">
  <img0
    jcr:primaryType="nt:unstructured"
    assetPath="741414711.jpg"
    excludedTypes="[]"/>
  <img1
    jcr:primaryType="nt:unstructured"
    assetPath="741414712.jpg"
    excludedTypes="[]"/>
</relatedImages>

I know that xstream allows to add list of objects to convert as list of xml nodes but in my case the list of nodes are same but node name differs. Let me know if you need more information.


Solution

  • /**
     * 
     * @param contentxml
     * @param xmlAttributeName
     * @param xmlAttributeValue
     * @return xml with inserted node value
     */
    private static String insertXmlAttributeValue(String xml, String xmlAttributeName, String xmlAttributeValue){
        if(StringUtils.isNotEmpty(xmlAttributeValue)){
            String xmlAttributeKey = xmlAttributeName + "=\"\"";
            xmlAttributeValue = xmlAttributeName + "=\""+ xmlAttributeValue+"\"" ;
            xml = xml.replace(xmlAttributeKey, xmlAttributeValue);
        }
        return xml;
    }