Hi Klaus !
               this solved the problem,
          

            OutputFormat of = new OutputFormat();
            of.setIndent(1);
            of.setIndenting(true);
            //of.setDoctype(null," users.dtd");
            XMLSerializer serializer = new XMLSerializer(fos,of);
    //         SAX2.0 ContentHandler.
            ContentHandler hd = serializer.asContentHandler();
           
            //ContentHandler hd = new DefaultHandler();
            hd.startDocument();

    //         Processing instruction
              hd.processingInstruction("qbxml","version=\"4.0\" ");



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,

    private void buildNamesXMLFile()
    {
        try
        {
       
            FileOutputStream fos = new      
            FileOutputStream("QBXMLRequest.xml");
           
    //         XERCES 1 or 2 additionnal classes.
            OutputFormat of = new OutputFormat();
            of.setIndent(1);
            of.setIndenting(true);
            //of.setDoctype(null,"users.dtd ");
            XMLSerializer serializer = new XMLSerializer(fos,of);
    //         SAX2.0 ContentHandler.
            ContentHandler hd = serializer.asContentHandler();
           
            //ContentHandler hd = new DefaultHandler();
            hd.startDocument();
    //         Processing instruction
            hd.processingInstruction("qbxml","version=\"4.0\" ");
                       
            //         USER attributes.
            AttributesImpl atts = new AttributesImpl();
    //         USERS tag.
            hd.startElement("","","QBXML",atts);
           
            atts.clear();
            atts.addAttribute("","","onError","CDATA","stopOnError");
            hd.startElement("","","QBXMLMsgsRq",atts);

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

 
            hd.endElement("","","QBXMLMsgsRq");
            hd.endElement("","","QBXML");                       
       
            hd.endDocument ();
            fos.close();
           
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

       
    }


and here is the method to read it from File and then convert it to String,

public String namesQuery()
    {
   
        // A call to the above method to genrate XML and serialize it to File
        buildNamesXMLFile();
        
        String xml;
       

        // read File in input Source then convert it to String.
        try
        {
            InputSource inputSource = new InputSource("QBXMLRequest.xml");
            Source source = new SAXSource(inputSource);
            
            StringWriter out = new StringWriter();
            Result result = new StreamResult(out);
   
            TransformerFactory tFactory = TransformerFactory.newInstance();
            Transformer transformer = tFactory.newTransformer();
             transformer.transform(source, result);
            
            xml = out.toString();              
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return "";
        }
   
        return xml;        
    
    }      
        
  
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:


public class ParseNameResponse extends DefaultHandler
{

     Vector namesList = new Vector();
     
   
    public ParseNameResponse()
    {
        super();       
    }
    

    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 = new ParseNameResponse();
        // Use the default (non-validating) parser
        SAXParserFactory factory = SAXParserFactory.newInstance();
        try
        {
           
            //             Parse the input
            SAXParser saxParser = factory.newSAXParser();          
            saxParser.parse(source,handler);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return namesList;
    }
   
       
   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.

  
    public void characters(char buf[], int offset, int len)  
    {
        try
        {
            String s = new String(buf, offset, len);
           
            if (( !s.trim().equals("")) )
            {
                namesList.add(s);              
            }
           
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
       
    }

  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.

take care,
Babar Abbas.

            

On 9/10/06, Klaus Malorny <[EMAIL PROTECTED]> wrote:
Babar Abbas wrote:
> Hi Michael and Klaus!
>                        As stated by you there is a difference b/w Prolog
> and Processing Instruction:
>
> Prolog:
>             An XML document starts with a Prolog, The prolog is used to
> signal the beginning of the XML data, describe its character encoding
> method, and provide some other configuration hints to the XML parser and
> application.
>             The prolog consists of an optional XML declaration, which
> can be ommitted, and the document can still be well formed.
>
>          "In my case I want to have 2 prolog elements, one is the
>                       <?xml version="1.0" encoding="UTF-8" ?>
>           and the other one is
>                       <?qbxml version="2.0" ?> "

> [...]

>             hd.processingInstruction("qbxml version","2.0 ");
>
>            can klaus and Michael help, I think enough debate till now,
> which method of which class should I call, there is an OutputFormat
> class, but I have seen its functions, couldn't be of much help.
>                      any help will be appreciated. I came across this
> page, which was of much help in explaining how to generate, XML with SAX
> and XERCES.
>
>             http://www.javazoom.net/services/newsletter/xmlgeneration.html
>



Hi,

try

   ProcessingInstruction pi =
     document.createProcessingInstruction ("qbxml", "version=\" 2.0\"");

instead.

While your processing instruction has an attribute-like syntax, this is not part
of the XML specification. The XML specification allows any text behind the
target (except the end marker of the processing instruction of course).
Therefore, there is no support for attribute-like content for processing
instructions in all toolkits I know (including DOM and SAX) and you have to
create this by hand, like I did it in the statement above.

Hope that helps.

Regards,

Klaus

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


Reply via email to