Thanks Klaus !
              both of my problems are solved, for serializing the XML to string I am using StringWriter, and passing it to the XMLSerializer class,

           StringWriter out = new StringWriter();
           
    //         XERCES 1 or 2 additionnal classes.
            OutputFormat of = new OutputFormat();
            of.setIndent(1);
            of.setIndenting(true);
   
            XMLSerializer serializer = new XMLSerializer(out,of);

    //         SAX2.0 ContentHandler.
            ContentHandler hd = serializer.asContentHandler ();

            .............................................

           hd.endDocument();
           xml = out.toString();

         this returned me the xml in string format.

For the second problem, I was mistakenly, using a new instance of ParseNameResponse, which extends DefaultHandler, now this instance is used instead.
 
public class ParseNameResponse extends DefaultHandler
{

     Vector namesList = null ;
     
   
    public ParseNameResponse()
    {
        super();
        this.namesList = new Vector();
    }
   
   
    public Vector nameResponse(String responseXML)
    {       
        Reader reader = new BufferedReader(new StringReader(responseXML));
        InputSource source = new InputSource(reader);       
       
        //         Use an instance of ourselves as the SAX event handler
        DefaultHandler handler = this;
        // Use the default (non-validating) parser
        SAXParserFactory factory = SAXParserFactory.newInstance();
        try
        {
           
            //             Parse the input
            SAXParser saxParser = factory.newSAXParser();
           
            saxParser.parse(source,handler);
    
            ...........................................


     this solved the problem.
 
                since both of my problems are solved, my app is running very well, thanks to Klaus, for the parsing of XML, I will now use DOM, instead of SAX, as it is error prone. It is not gauranteed, that the characters method will return the whole element string, in one piece. DOM should solve that problem.

thanks,
babar.                                 

On 9/16/06, Klaus Malorny <[EMAIL PROTECTED] > wrote:
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