2

Below is the input and expected output xml. I'm using JDOM parser

Input:

<emp:transactions xmlns:emp="http://www.emp.com/dept"   
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="">
    <emp:transaction xmlns:dept="http://www.emp.com/emp">   
        <emp:empBooking emp:bookingNbr=""/>
    </emp:transaction>  
</emp:transactions>

Expected Output:

<emp:transactions xmlns:emp="http://www.emp.com/dept" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="">
    <emp:transaction xmlns:dept="http://www.emp.com/emp">   
        <emp:empBooking emp:bookingNbr=""/>
        <emp:flexStringFields>
            <emp:flexString01>emp flex 1</emp:flexString01>
        </emp:flexStringFields>
    </emp:transaction>  
</emp:transactions>

Code:

SAXBuilder builder = new SAXBuilder();
Reader in = new StringReader(xmlAsString);
Document document = (Document)builder.build(in);
Element rootNode = document.getRootElement();
List<Element> list = rootNode.getChildren (
    "transaction", 
    Namespace.getNamespace("NS1", "http://www.emp.com/dept")
);
for(Element transaction: list) {
    // ...
}

I'm not sure how to add the element emp:flexStringFields with namespace emp with its child emp:flexString01

Could you please help me on how to add it?

TT.
  • 15,774
  • 6
  • 47
  • 88
Kathiresa
  • 75
  • 7
  • Possible duplicate of [How do I add a namespace prefix to an XML DOM object?](http://stackoverflow.com/questions/10995338/how-do-i-add-a-namespace-prefix-to-an-xml-dom-object) – har07 Mar 05 '16 at 01:43

1 Answers1

0

The following works fine:

Element flexStringField = new Element (
    "flexStringFields", 
    "emp", 
    Namespace.getNamespace("NS1", "http://www.emp.com/dept").getURI()
);
content.addContent(flexStringField);
TT.
  • 15,774
  • 6
  • 47
  • 88
Kathiresa
  • 75
  • 7