Babar Abbas wrote:
Hi Klaus !
[...]

but now I have a few more problems !
After generating the contents of the XML message, I need to convert it to String, right now I am serializing it to the File, and then reading it back in an input source (SAXSource), then with the help of the javax.xml.transform package converting it to the String. can't I directly transform the generated XML into String without serializing to the File first.

Here is the method that generates and serializes,


> [...]

That's quite complicated what you are doing, and, of course, completely 
superfluous!

You already used the right class, StringWriter, but in a wrong context. You do not need to write to a file. You can use the StringWriter class exactly for your purpose. The XMLSerializer has also a constructor that takes a writer, so you can use an instance of the StringWriter class here also. After the "hd.endDocument ();" statement in your code, you can simply call the toString () method and you have your XML as a string.

The second Problem is, I am getting the response from a program in the form of a String, I convert it to XML with the help of Reader and StringReader, then parse it, and extract the element contents (characters) out of XML and storing in the vector, the following class initializes the SAX Library and parses the String XML:



> [...]
as you can see the Vector namesList is an instance variable, and being intialized as the time of declaration, but as I add contents to the vector, in the characters method, which the SAX parser calls, on the time parsing the instance Vector namesList doesn't get updated, so when I return the vector, from the above nameResponse method the size of the vector is 0. the following is the characters method, in which I add to the namesList Vector.

[...]

as I println the size of the vector in the characters method its increasing, means that the objects are being added to the Vector, but when I get back in the initial nameResponse method afte the parsing is completed, the size of the vector is 0 again. Moreover if initialize the vector in the nameResponse method, the characters method shows a nullPointerException, as if the vector hasn't been initialized.
                     any help regarding this specially the second problem.



There are several issues here:

1. Unless you are stuck to an old Java version, I wouldn't use the Vector
   class any longer, but interfaces and classes of the Java Collections
   Framework. They are simply more flexible. Anyhow.

2. The problem that you get an empty result is simply caused by the fact
   that you use two different instances of your class. The one ("handler")
   is used by you to accept the SAX calls, and this fills its Vector
   instance. However, at the end of the method, you return the Vector
   of the "this" instance. You can simply omit the creation of the
   extra instance and use "saxParser.parse (source, this);"

3. As told over and over again in this list, there is a general
   misconception that the "characters" method would always report
   the whole text between two tags in one piece. THIS IS NOWHERE ASSURED.
   If you have, for example

     <msg>Hello World</msg>

   it could be that the text is reported in multiple calls, for example
   in three pieces, "Hell", "o Worl", "d". In your case, this would result
   in three elements in your vector. Unless you are an expert in
   writing pushdown automata, I would recommend that you use DOM or JDOM
   instead of SAX for analysing an XML document. It is much less error-prone.


Regards,

Klaus

take care,
Babar Abbas.



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to