XMLStreamWriter issue

Hello,

I'm using http://java.sun.com/webservices/docs/1.5/api/javax/xml/stream/XMLStreamWriter.html

and when I try to do something like

XMLStreamWriter writer = createXMLStreamWriter(someoutbutbuffer)

String somestring ="<abc>hello</abc>

writer.writeCharacters(somestring);

the someoutbutbuffer ends up actually having the following in it

<abc>hello</abc>

As I understand it that's some sort of parsing being done... but how do I make it not do that? Is there some way to put in escape characters of some kind before each <?

[622 byte] By [zkajana] at [2007-11-27 10:59:24]
# 1

That looks reasonable enough to me.

But perhaps you didn't escape the special characters in your post enough and you really meant to say that the result was this:&lt;abc&gt;hello&lt;/abc&gt;Yes?

Well, if you didn't want that but you really wanted to write an abc element, then you should use the writeElement() method to do so.

DrClapa at 2007-7-29 12:22:52 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

ooooopsie, yeah, it looked closer to yours actualy,

The following but without the spaces is what it looked like

& l t ; abc>hello& l t ; /abc>

(hopefuly that appears the way i intend it to)

I'm aware of writeElement, problem is that I'm trying to make a little debug tool where I can paste or type any raw xml into a text box and then have it be wrapped up within a certain place of the rest of my usual xml message, send it out, and be undistinguishable at the receiving end from one of my usual messages where all the tags are put together with writeElement type calls.

zkajana at 2007-7-29 12:22:52 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3

Then you're going to have to parse that String into an XML document before you serialize it to the XMLStreamWriter. You have seen that treating it as a plain string doesn't do what you want.

The other benefit of parsing the String is that you will catch malformed XML before blindly inserting it into your output.

DrClapa at 2007-7-29 12:22:52 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4

Thank you.

I got a parser going on now by writing the String to a temporary file, then parsing that file into a Node. Works well. Now going from the Node and steping through it recursively to put out to the stream works well too.. in all but one of my test cases, but I don't know if that one case actualy conforms to correct XML itself actually, so it might be ok.

Basicaly, all the following go through ok:

<a><b></b></a>

<a>hello</a>

<a><b>hello</b></a>

even

<a><b></b><c></c></a>

however,

<a>hello<b></b></a>

does not.

What happened is that if I had something like

<a>hello<b>bye</b></a>

and then called getNodeText () on the node for which getNodeName() returned "a", I would get "hellobye" instead of "hello". So in my parsing out I made it only get the text if there is only one child showing up and it's name is "#name". Which means that when that String is put in (<a>hello<b>bye</b></a>) I get <a><b>bye</b></a> going into the stream.

Well, my first question is, is <a>hello<b>bye</b></a> (or for that matter <a><c>somethingElse</c>hello<b>bye</b></a>, etc...) a valid XML I should be able to parse?

And if so, is there something that will get me the text of a specific Node without appending the text of all the subnodes (and their subnodes, etc..)?

Thanks a bunch!

p.s. I found in another thread http://www.w3schools.com/xml/xml_validator.asp and checked that and apparently it is valid. So now the question is how to get that text without geting the text for the all the subnodes as well.

Message was edited by:

zkajan

for

mispelled words, forgotten quotes

zkajana at 2007-7-29 12:22:52 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5

Hrrrm, I made another reply here but looks like the forums had a rollback.

Anyway, I did what you suggested, but I'm having one (pretty minor) problem. All the parsing goes fine, but I have a problem with calling getNodeText() on a Node returning both the text of itself and also of all descendants. Is there a way to get the text of just the node, without descendants?

Meaning, currently, if I have:

<a>hello<b>hi<c>bye></c></b></a>

If I'm at node whose getNodeName() returns "a" and call getNodeText() I get "hellohibye".

If I'm at node whose getNodeName() returns "b" and call getNodeText() I get "hibye".

If I'm at node whose getNodeName() returns "c" and call getNodeText() I get "bye".

What I want to do is:

<a>hello<b>hi<c>bye></c></b></a>

If I'm at node whose getNodeName() returns "a" and call mysteryfunction() I get "hello".

If I'm at node whose getNodeName() returns "b" and call mysteryfunction() I get "hi".

If I'm at node whose getNodeName() returns "c" and call mysteryfunction() I get "bye".

What mysteryfunction() if any would allow me to do that?

Message was edited by:

zkajan

ofcourse, right after I re-make a post, my previous one re-appears. Ok, well, i'll leave both in for more clarity.

zkajana at 2007-7-29 12:22:52 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 6

That's called "mixed content" and it's perfectly valid XML.

> So now the question is how to get that text without geting the text for the all the subnodes as well.

No, it isn't. Or it shouldn't be. It's perfectly possible for a node to contain a list of subnodes which in turn contain list of subnodes. You have to parse the tree fully. No shortcuts.

DrClapa at 2007-7-29 12:22:52 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...