XML and Java from Beginner

Discussion in 'Java' started by privateson@hotmail.com, May 15, 2007.

  1. Guest

    Hi,

    I am a Beginner, and need some help.
    I am using .Net service and Java.

    One of my webMethod can return an array of integer,
    and the xml will looks like this:

    <returnIntsResult>
    <int>1</int>
    <int>2</int>
    <int>3</int>
    </returnIntsResult>

    How can I read all those integers?

    Right now I am using the following java code and can only read the
    first integer:

    try {
    // Since the Body.unmarshall() handler is static, we can't
    // replace the basic machinery easily. Instead, we must obtain
    // and parse the message on our own.
    this.soapMessage_ = this.message_.receive();
    XMLReader reader =
    (XMLReader)Class.forName("org.apache.xerces.parsers.SAXParser").newInstance();
    SAXHandler handler = new SAXHandler();
    handler.setElementToSearchFor ("returnIntsResult");

    // Set the Content Handler
    reader.setContentHandler (handler);


    // Parse the file
    reader.parse ( new InputSource (new StringReader
    (this.soapMessage_.getContent().toString() )));

    // If we reached here, the result has been parsed and
    // stored in the handler instance.
    for(int i=0; i<3; i++){
    returnValue="";
    returnValue = handler.getResult();

    }
    }
    catch (Exception exception) {
    ...........


    Thank you for your help
    , May 15, 2007
    #1
    1. Advertising

  2. wrote:
    > I am a Beginner, and need some help.
    > I am using .Net service and Java.
    >
    > One of my webMethod can return an array of integer,
    > and the xml will looks like this:
    >
    > <returnIntsResult>
    > <int>1</int>
    > <int>2</int>
    > <int>3</int>
    > </returnIntsResult>
    >
    > How can I read all those integers?


    The code attached below can parse that XML.

    But is it a web service ?

    If it is then you should not be parsing the XML manual. You
    should generate a client stub from the WSDL so you can call
    a method and get an int array returned without you having
    to know anything about XML.

    Arne

    ==============================================

    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.ArrayList;

    import javax.xml.parsers.FactoryConfigurationError;
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;

    import org.xml.sax.Attributes;
    import org.xml.sax.SAXException;
    import org.xml.sax.XMLReader;
    import org.xml.sax.helpers.DefaultHandler;

    public class SAXDemo {
    public static void main(String[] args) {
    ArrayList<String> result = new ArrayList<String>();
    try {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    SAXParser sp = spf.newSAXParser();
    XMLReader xr = sp.getXMLReader();
    xr.setContentHandler(new MySaxParser(result));
    xr.parse("C:\\demo.xml");
    } catch (FactoryConfigurationError e) {
    e.printStackTrace();
    } catch (ParserConfigurationException e) {
    e.printStackTrace();
    } catch (SAXException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    for(String item : result) {
    System.out.println(item);
    }
    }
    }

    class MySaxParser extends DefaultHandler {
    private StringBuffer element = new StringBuffer();
    private ArrayList<String> result;
    public MySaxParser(ArrayList<String> result) {
    this.result = result;
    }
    public void characters(char buf[], int offset, int len) throws
    SAXException {
    element.append(new String(buf, offset, len));
    return;
    }
    public void startElement(String namespaceURI, String localName,
    String rawName, Attributes atts) throws SAXException {
    if (rawName.equals("int")) {
    element = new StringBuffer();
    }
    return;
    }
    public void endElement(String namespaceURI, String localName, String
    rawName) throws SAXException {
    if (rawName.equals("int")) {
    result.add(element.toString());
    }
    return;
    }
    }
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, May 19, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=

    No Class at ALL!!! beginner/beginner question

    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, Feb 2, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    570
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=
    Feb 3, 2005
  2. Paul McFadden
    Replies:
    2
    Views:
    460
    Stanimir Stamenkov
    Feb 15, 2005
  3. Rensjuh
    Replies:
    7
    Views:
    962
    Mabden
    Sep 2, 2004
  4. william nelson

    Beginner's Beginner

    william nelson, Apr 11, 2011, in forum: Ruby
    Replies:
    7
    Views:
    211
    7stud --
    Apr 12, 2011
  5. Erik Wasser
    Replies:
    5
    Views:
    445
    Peter J. Holzer
    Mar 5, 2006
Loading...

Share This Page