0

I have problem when using JOOX for transforming XMLs. I am doing some changes to element matching the fieldTag

I have a code as following:

   Match xml = $(new StringReader(content.toString()));
        Match find = xml.find(fieldTag);
        Iterator<Element> iterator = find.iterator();
        while(iterator.hasNext()){
            Element next = iterator.next();
            String text = $(next).text();
            if(text.length() == 0) continue;

            next.setNodeValue("....");
        }

    return xml.toString();

However , for example an XML like this is input:

<Body>
<A>ABC</A>
<B></B>
</Body>

this is what i get as result:

<Body>
<A>transformed</A>
<B/>
</Body>

You can notice that Element B is a self closed XML tag. Does anyone know that how do I make it to be <B></B ??

Seng Zhe
  • 613
  • 1
  • 11
  • 21

1 Answers1

1

Why does it matter? Anyone who reads the resulting document with an XML parser won't notice the difference, and anyone who reads it with something that isn't an XML parser needs to be re-educated. Most XML serializers don't give you any control over such details, any more than they allow you to choose whether to have single quotes or double quotes around your attribute values.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • one of the requirement when communicating with a legacy system with XML is this. – Seng Zhe Jun 17 '16 at 07:46
  • Well, as soon as you start saying "one of our systems doesn't really do XML properly so everyone that talks to it is going to have to adjust", then you're losing all the benefits of using an open standard and you are vastly increasing your costs. If you really can't change the legacy system to understand real XML, then front-end it with a gateway that translates real XML to its restricted dialect. – Michael Kay Jun 17 '16 at 08:32