0
votes

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

2
using xslt might work as wellrogerdpack

2 Answers

0
votes

What you're asking for doesn't make sense. If the wanted document is parsed with a namespace aware parser, it is invalid since there is no namespace declaration for the peci prefix. If it is parsed with a namespace unaware parser, the document is valid, but then the root element does not have any namespace and neither does the other elements, but their local names has changed.

Is this really what you want?

UPDATE

I saw from your (deleted) answer that perhaps what you want is just to change the namespace for all elements. In that case this might help:

... 
Document d = new SAXBuilder().build(...);
updateNamespace(d.getRootElement(), Namespace.getNamespace("peci", "urn:com.uri/peci"));
...

static void updateNamespace(Element e, Namespace ns) {
    e.setNamespace(ns);
    for(Element child: e.getChildren()) {
        updateNamespace(child, ns);
    }
}

After that, you can filter out the elements that you don't want in the result document.

0
votes
    Namespace namespace_xsi = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
            Namespace namespace_ext = Namespace.getNamespace("ext", "urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2");

    // create the jdom
    Document jdomDoc = new Document();
    // create root element
    Element rootElement = new Element("Invoice", namespace_xsi);
            rootElement.addNamespaceDeclaration(namespace_ext);