2

I searched in SO but I did not found nothing that solves my problem. I hope some one can help me.

I am building a XML file and I need to remove the Namespace xmlns.

That is my code

Document xmlDocument = new Document();
Namespace ns1 = Namespace.getNamespace("urn:iso:std:iso:20022:tech:xsd:pain.001.001.03");
Namespace ns2 = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
Element root = new Element("Document", ns1);
root.addNamespaceDeclaration(ns2);
xmlDocument.setRootElement(root);
Element CstmrCdtTrfInitn = new Element("CstmrCdtTrfInitn");
root.addContent(CstmrCdtTrfInitn);

PrintDocumentHandler pdh = new PrintDocumentHandler();
pdh.setXmlDocument(xmlDocument);
request.getSession(false).setAttribute("pdh", pdh);

ByteArrayOutputStream sos = new ByteArrayOutputStream();
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
Format format = outputter.getFormat();
format.setEncoding(SOPConstants.ENCODING_SCHEMA);
outputter.setFormat(format);
outputter.output(root, sos);
sos.flush();
return sos;

And this is the created XML-File

<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03">
    <CstmrCdtTrfInitn xmlns=""/>
</Document>

I have to remove the namespace xmlns from the tag CstmrCdtTrfInitn.

Many thanks in advance.

har07
  • 88,338
  • 12
  • 84
  • 137
user2960190
  • 180
  • 1
  • 4
  • 16
  • Without the `xmlns=""` it would be in namespace `urn:iso:std:iso:20022:tech:xsd:pain.001.001.03`. Is that what you want? – Henry Apr 27 '16 at 10:44
  • No, not the namespace in the Document tag. I have to remove only the namespace from the CstmrCdtTrfInitn tag. – user2960190 Apr 27 '16 at 10:53
  • 1
    Still not clear. As the document is currently, `CstmrCdtTrfInitn` does not have a namespace. If you remove the `xmlns=""`attribute, this element would be in namespace `urn:iso:std:iso:20022:tech:xsd:pain.001.001.03` which is inherited from the root element. – Henry Apr 27 '16 at 10:58

2 Answers2

2

Namespace declaration without prefix (xmlns="...") is known as default namespace. Notice that, unlike prefixed namespace, descendant elements without prefix inherit ancestor's default namespace implicitly. So in the XML below, <CstmrCdtTrfInitn> is considered in the namespace urn:iso:std:iso:20022:tech:xsd:pain.001.001.03 :

<Document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03">
    <CstmrCdtTrfInitn/>
</Document>

If this is the wanted result, instead of trying to remove xmlns="" later, you should try to create CstmrCdtTrfInitn using the same namespace as Document in the first place :

Element CstmrCdtTrfInitn = new Element("CstmrCdtTrfInitn", ns1);
har07
  • 88,338
  • 12
  • 84
  • 137
0

I did some code for you, I dont know what package You are using... If You only want to remove the xmlns attribute from the CstmrCdtTrfInitn tag try with this code as other method to generate XML (I just modified this):

    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

    // root elements
    Document doc = docBuilder.newDocument();
    Element rootElement = doc.createElement("Document");
    rootElement.setAttribute("xmlns", "urn:iso:std:iso:20022:tech:xsd:pain.001.001.03");
    rootElement.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");

    doc.appendChild(rootElement);

    // staff elements
    Element CstmrCdtTrfInitn = doc.createElement("CstmrCdtTrfInitn");
    rootElement.appendChild(CstmrCdtTrfInitn);


    // write the content into xml file
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(doc);

    // Output to console for testing
    StreamResult result = new StreamResult(System.out);

    transformer.transform(source, result);

  } catch (ParserConfigurationException pce) {
    pce.printStackTrace();
  } catch (TransformerException tfe) {
    tfe.printStackTrace();
  }
Servio
  • 63
  • 1
  • 8