I tried to construct an xml document with JDOM:
Node N =Effective_Change.getElementsByTagName("xml-fragment").item(0);
NewElement=N.getOwnerDocument().createElementNS(namespace, N.getNodeName());
Namespace sNS =Namespace.getNamespace(prefix,namespace);
list = N.getChildNodes();
NodeList ListInter=null;
org.jdom.Element subroot=null;
for(int c=1;c<list.getLength();c++) {
if(!(list.item(c).getNodeName().equals("#text"))){
if(list.item(c).getChildNodes().getLength()>1) {
System.out.println("true : "+list.item(c).getChildNodes().getLength());
}
else {
subroot=new org.jdom.Element(list.item(c).getNodeName(), sNS);
subroot.addContent(list.item(c).getTextContent());
}
root.addContent(subroot);
XMLOutputter outp = new XMLOutputter();
String s = outp.outputString(root);
System.out.println(s);
}
}
My input xml string :
<xml-fragment>
<Derived_Event_Code>xx</Derived_Event_Code>
<Effective_Moment>2018-07-23T04:20:04</Effective_Moment>
<Entry_Moment>2018-07-23T04:20:04</Entry_Moment>
<Person_Identification isUpdated="1">
<Government_Identifier isDeleted="1">
<Government_ID>xxxx</Government_ID>
<Government_ID_Type>xxxx</Government_ID_Type>
<Issued_Date>xxxx</Issued_Date>
</Government_Identifier>
</Person_Identification>
</xml-fragment>
The actual output :
<xml-fragment xmlns="urn:com.uri/peci">
<peci:Derived_Event_Code xmlns:peci="urn:com.workday/peci">DTA</peci:Derived_Event_Code>
<peci:Effective_Moment xmlns:peci="urn:com.workday/peci">2018-07-23T04:20:04</peci:Effective_Moment>
<peci:Entry_Moment xmlns:peci="urn:com.workday/peci">2018-07-23T04:20:04</peci:Entry_Moment>
</xml-fragment>
and I want the output like this code below: the namespace just for the root tag, in this case for xml-fragment and for the rest of tags I want just the prefix without name space and without the attribute xmlns:"" for child tags.
<xml-fragment xmlns="urn:com.uri/peci">
<peci:Derived_Event_Code>xx</peci:Derived_Event_Code>
<peci:Effective_Moment>2018-07-23T04:20:04</peci:Effective_Moment>
<peci:Entry_Moment>2018-07-23T04:20:04</peci:Entry_Moment>
</xml-fragment>
and even when I tried
subroot.setNamespace(sNS.NO_NAMESPACE);
I got in child tags attribute xmlns:""
I need really help !!