Hi Michael,

I know <tag></tag> and <tag/> are the same, but unfortunately the buggy-parser-that-cannot-be-changed on the other end doesn't :)


DocumentBuilder db = dbf.newDocumentBuilder();
Document d = db.newDocument();
Element root = d.createElement("root");
Element tag = d.createElement("tag");
tag.setTextContent("");
d.appendChild(root);
root.appendChild(tag);
System.out.println(XmlUtils.formatXmlAsString(d));


This always outputs <tag/> and never <tag></tag> like I need it to.

- Ian.



On Dec 12, 2008, at 10:33 PM, Michael Glavassevich wrote:

Hi Ian,

> I need to create XML that looks like this whenever the value of
> "tag" is "" (the empty string):
>
> <root>
> <tag></tag>
> </root>

Why? <tag/> and <tag></tag> have the same meaning. Whichever form is chosen by the serializer should have no significance.

> I am more concerned in preserving the empty text node when I
> serialize to e.g. a file... not so much the parsing.
>
> Any one else have any ideas?

Would help if you showed your code for serializing the document.

> Are blank text nodes like that invalid XML or something?

In the snippet you posted you created a text node with the '\t' (tab) character in it. That isn't "blank" or empty.

Thanks.

Michael Glavassevich
XML Parser Development
IBM Toronto Lab
E-mail: mrgla...@ca.ibm.com
E-mail: mrgla...@apache.org

Ian Hummel <hum...@parityinc.net> wrote on 12/12/2008 09:21:47 AM:

> Hi, I didn't really understand how that's going to help.
>
> I am more concerned in preserving the empty text node when I
> serialize to e.g. a file... not so much the parsing.
>
> Any one else have any ideas?  Are blank text nodes like that invalid
> XML or something?
>
> On Dec 11, 2008, at 11:53 AM, ravika...@gmail.com wrote:
>
> Hi Lan,
>
> I think we can Implement by LSParser Interface. http://java.sun.
> com/j2se/1.5.0/docs/api/org/w3c/dom/ls/LSParser.html
> this link may help you.
>
> Regards,
> Ravikanth

> On Thu, Dec 11, 2008 at 7:36 PM, Ian Hummel <hum...@parityinc.net> wrote:
> Hi everyone,
>
> I need to create XML that looks like this whenever the value of
> "tag" is "" (the empty string):
>
> <root>
> <tag></tag>
> </root>
>
> I've tried the following:
>
> DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
> DocumentBuilder db = dbf.newDocumentBuilder();
> Document d = db.newDocument();
> Element root = d.createElement("root");
> Element tag = d.createElement("tag");
> d.appendChild(root);
> root.appendChild(tag);
> Text text = d.createTextNode("\t");
> tag.appendChild(text);
>
> but I always end up with XML like this:
>
> <root>
> <tag/>
> </root>
>
> Is there a way to force empty text nodes to get "denormalized" ?
>
> Thanks,
>
> Ian.
>
> --
> Ravikanth


Reply via email to