insertion of string into characters of parsed xml (in SAX)

Y

YuliaG

Hi,
I'm using SAX for XML parsing and inserting new elements (can't use
DOM).
I have a string that contains result of some "xml export to string",
for instance: "<a><b>xxxxxx</b></a>". I have to insert this string to
the file (as characters of some tag I'm adding) at a run time.

I tried:

Attributes attr = new AttributesImpl();
super.startElement(uri, "myData", "myData", attr);
String myData = "<a><b>xxxxxx</b></a>";
char[] myDataAsChars = myData.toCharArray();
super.characters(myDataAsChars ,0,myDataAsChars.length -1);
super.endElement(uri, "myData", "myData");

I need to get in file the following result:
<myData><a><b>xxxxxx</b></a></myData>

But, instead I get:
<myData>&lt;a&gt;&lt;b&lt;xxxxxx&lt;/b&gt;&lt;/a&gt;</myData>

How can I do this insertion correctly?
Thanks,
Yulia
 
A

Arnaud Berger

Hi,

You should probably better use also startElement and endElement for a and b,
rather than
printing them in a String (if they also are XML elements).

startElement(...myData...)
startElement(...a...)
startElement(...b...)
.....
.....characters(...xxxxxx...)
.....
endElement(...myData...)
endElement(...a...)
endElement(...b...)


If the problem is that <a> and <b> shouldn't appear as part of the XML
structure, you may consider
using CDATA sections.
http://access1.sun.com/techarticles/CDATA/CDATAArticle.html

Regards,

Arnaud
 
A

Arnaud Berger

Sorry,

read as:

endElement(...b...)
endElement(...a...)
endElement(...myData...)

Arnaud

Arnaud Berger said:
Hi,

You should probably better use also startElement and endElement for a and b,
rather than
printing them in a String (if they also are XML elements).

startElement(...myData...)
startElement(...a...)
startElement(...b...)
....
....characters(...xxxxxx...)
....
endElement(...myData...)
endElement(...a...)
endElement(...b...)


If the problem is that <a> and <b> shouldn't appear as part of the XML
structure, you may consider
using CDATA sections.
http://access1.sun.com/techarticles/CDATA/CDATAArticle.html

Regards,

Arnaud



YuliaG said:
Hi,
I'm using SAX for XML parsing and inserting new elements (can't use
DOM).
I have a string that contains result of some "xml export to string",
for instance: "<a><b>xxxxxx</b></a>". I have to insert this string to
the file (as characters of some tag I'm adding) at a run time.

I tried:

Attributes attr = new AttributesImpl();
super.startElement(uri, "myData", "myData", attr);
String myData = "<a><b>xxxxxx</b></a>";
char[] myDataAsChars = myData.toCharArray();
super.characters(myDataAsChars ,0,myDataAsChars.length -1);
super.endElement(uri, "myData", "myData");

I need to get in file the following result:
<myData><a><b>xxxxxx</b></a></myData>

But, instead I get:
<myData>&lt;a&gt;&lt;b&lt;xxxxxx&lt;/b&gt;&lt;/a&gt;</myData>

How can I do this insertion correctly?
Thanks,
Yulia
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top