Control over character encodings in XML

0 views
Skip to first unread message

Matt Adams

unread,
Nov 22, 2010, 10:40:50 PM11/22/10
to xml...@googlegroups.com
Hello:

I am curious to know if there is any way that I can have more control
over character encodings in the resulting XML produced by the XMLTag
toString() method.

For example, most of the time xmltool will output what I would usually
expect. For example,

<hint>only numbers > 10.51 and < 18.39</hint>

becomes

<hint>only numbers &gt; 10.51 and &lt; 18.39</hint>

But sometimes, I have an XML doc that includes something like

<label>review widget. is your email still <output
value="/widgets/regex"/>?</label>

and when this string is parsed and then written out again it becomes

<label>review widget. is your email still &lt;output
xmlns="http://www.w3.org/2002/xforms" value="/widgets/regex"/&gt;?</label>

which is definitely not what I am looking for.

I would appreciate any suggestions you might have for working around
this problem.


Thanks,

Matt

Mathieu

unread,
Nov 22, 2010, 11:20:34 PM11/22/10
to xmltool
Hi,

By first checking, i dont't think XmlTool can do this. The simple
reason is that when you add some text in a node (JDK W3C Element),
XmlTool creates a TextNode (JDK W3C implementation). This
transformation is not done by XmlTool but directly by the JDK.

If you need to have an element <output> within an element <label>, you
have:

* either to create it manually
* either to use a CDATA section so that you can have the raw text
not transformed
* or the trick would be tocreate another XMLTag in an exising one
like this:

XMLTag tag = XMLDoc.from("<root/>", true);
String rawText = "review widget. is your email still <output
value=\"/widgets/regex\"/>?";
tag.addTag(XMLDoc.from("<label>" + rawText + "</label>",
true));
System.out.println(tag.gotoRoot());

outputs:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
<label>review widget. is your email still <output value="/widgets/
regex"/>?</label>
</root>

But if you are using namespaces, i don't know how much the insertion
of a W3C Document within another one is reliable...

Hope it helps,

Mat'

Matt Adams

unread,
Nov 23, 2010, 8:16:53 PM11/23/10
to xml...@googlegroups.com
Mathieu wrote:

> the trick would be to create another XMLTag in an existing one

This solution worked perfectly once I disabled namespaces for the XMLDoc
in tag.addTag(). Thank you very much!


Matt

Reply all
Reply to author
Forward
0 new messages