0

I'm using spring java to parse an xml.

The xml contains the following element:

 <component xmlns="">
      <nonXMLBody classCode="DOCBODY" moodCode="EVN">
         <text mediaType="application/pdf" representation="B64">zzz</text>
      </nonXMLBody>
 </component>

I need to remove the attribute xmlns="".

I'm have the following code but the attribute xmlns="" is still there.

Document dom = null;
try {
                    dom = xmlDocBuilder.parse(inpSource);
                } catch (SAXException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
NodeList nodeComponent = dom.getElementsByTagName("component");
Element element = (Element) nodeComponent.item(0);
element.removeAttribute("xmlns");
zinon
  • 4,427
  • 14
  • 70
  • 112
  • 1
    "with no luck" is a pretty imprecise description on what happened and why it didnt meet your expectations... – Gyro Gearless Apr 22 '21 at 07:41
  • @GyroGearless I mean that the xml is as it is. `xmlns` attribute is still here. – zinon Apr 22 '21 at 07:46
  • `xmlns` is a *very special* attribute used to define the **XML namespace**. The unnamed namespace defined by `xmlns=...` also applies to all the elements nested inside the given element. Are you trying to remove the namespace for *all* the elements in the XML? Do you know what namespaces are in XML? Are you aware of what you're asking? – Andreas Apr 22 '21 at 07:59
  • @Andreas I have one more `xmlns` in my document. However, I need to remove this specific one only. I'm creating a `cda` patient summary document and the validator does not accept `xmlns` for `component` element not for the nested elements. – zinon Apr 22 '21 at 08:01
  • Does the other `xmlns` give a different value, i.e. not the `""` empty string? – Andreas Apr 22 '21 at 08:05
  • @Andreas Yes. The other one is `xmlns="urn:hl7-org:v3"`. – zinon Apr 22 '21 at 08:06
  • 1
    That means you need to **rename** the elements (`component`, `nonXMLBody`, and `text`). Oh, too bad, elements cannot be renamed. Well then, you need to remove the elements and re-create them with the correct qualified names, while retaining all other attributes and content. That's a lot of work. Maybe fixing the code that created the XML, to do it right in the first place, should be considered. – Andreas Apr 22 '21 at 08:09

1 Answers1

0

Based on @Andreas comment and this solution I found my solution.

As I set in the comment above I have another one xmlns attribute in the beginning of my xml document which is xmlns="urn:hl7-org:v3".

So, in order to have the same namespace in my new elements while creating them, I'm using the following code:

Element root = dom.getDocumentElement();
Element componetEl = dom.createElement("component");
componetEl.setAttribute("xmlns", "urn:hl7-org:v3"); //this is the solution
root.appendChild(componetEl);
Element nonXMLBodyEl = dom.createElement("nonXMLBody");
nonXMLBodyEl.setAttribute("xmlns", "urn:hl7-org:v3"); //this is the solution
...
componetEl.appendChild(nonXMLBodyEl);
Element textEl = dom.createElement("text");
textEl.setAttribute("xmlns", "urn:hl7-org:v3"); //this is the solution
nonXMLBodyEl.appendChild(textEl);

...

Thus, now my resulted xml is:

<component>
      <nonXMLBody classCode="DOCBODY" moodCode="EVN">
         <text mediaType="application/pdf" representation="B64">zzz</text>
      </nonXMLBody>
 </component>
zinon
  • 4,427
  • 14
  • 70
  • 112