Can I read String (XML content) rather XML file using SAX parser

Discussion in 'Java' started by Sanjeev, May 2, 2008.

  1. Sanjeev

    Sanjeev Guest

    Details as follows

    1> account.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <Account>
    <Customer>
    <AcctNo>9774</AcctNo>
    <Name>
    <FirstName>Sanjeev</FirstName>
    <MiddleName>Narayan</MiddleName>
    <LastName>Atawankar</LastName>
    </Name>
    <DOB>08-Dec-1982</DOB>
    <Balance>50000</Balance>
    </Customer>
    ......
    </Account>

    2> Sample java Code
    .....
    private void parseDocument() {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    try {
    SAXParser sp = spf.newSAXParser();
    sp.parse("account.xml", this);
    }catch(SAXException se) {
    se.printStackTrace();
    }catch(ParserConfigurationException pce) {
    pce.printStackTrace();
    }catch (IOException ie) {
    ie.printStackTrace();
    }
    }
    ......

    I am getting String from third party i.e. content of above xml
    document/file in string.
    my requirement is instead of reading from xml file, I need to read it
    from String.

    Could any one help me.

    Thanking in Advance
    Sanjeev
     
    Sanjeev, May 2, 2008
    #1
    1. Advertising

  2. Sanjeev

    Sanjeev Guest

    On May 2, 11:59 am, "Timir Hazarika" <> wrote:
    > Try this:
    >
    > sp.parse(  new InputSource(new ByteArrayInputStream(string.getBytes()))  );
    >
    > - Timir
    >
    > "Sanjeev" <> wrote in message
    >
    > news:...
    >
    >
    >
    > > Details as follows

    >
    > > 1> account.xml
    > > <?xml version="1.0" encoding="UTF-8"?>
    > > <Account>
    > >  <Customer>
    > >        <AcctNo>9774</AcctNo>
    > >          <Name>
    > >     <FirstName>Sanjeev</FirstName>
    > > <MiddleName>Narayan</MiddleName>
    > > <LastName>Atawankar</LastName>
    > >   </Name>
    > >        <DOB>08-Dec-1982</DOB>
    > >        <Balance>50000</Balance>
    > >  </Customer>
    > > .....
    > > </Account>

    >
    > > 2> Sample java Code
    > > ....
    > > private void parseDocument() {
    > > SAXParserFactory spf = SAXParserFactory.newInstance();
    > > try {
    > > SAXParser sp = spf.newSAXParser();
    > > sp.parse("account.xml", this);
    > > }catch(SAXException se) {
    > > se.printStackTrace();
    > > }catch(ParserConfigurationException pce) {
    > > pce.printStackTrace();
    > > }catch (IOException ie) {
    > > ie.printStackTrace();
    > > }
    > > }
    > > .....

    >
    > > I am getting String from third party i.e. content of above xml
    > > document/file in string.
    > > my requirement is instead of reading from xml file, I need to read it
    > > from String.

    >
    > > Could any one help me.

    >
    > > Thanking in Advance
    > > Sanjeev- Hide quoted text -

    >
    > - Show quoted text -

    Thanks.

    I have made the following changes.
    xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
    "\n<Account>" +
    "\n <Customer>" +
    "\n <AcctNo>9774</AcctNo>" +
    "\n <Name>" +
    "\n <FirstName>Sanjeev</FirstName>" +
    "\n </Name>" +
    "\n <Balance>50000</Balance>" +
    "\n </Customer>"+
    "\n</Account>";
    System.out.println("XML String = " + xmlString);
    sp.parse(new InputSource(new
    ByteArrayInputStream(xmlString.getBytes())));

    But I am getting the foolowing error.
    IFXParser.java:56: cannot find symbol
    symbol : class InputSource
    location: class IFXParser
    sp.parse(new InputSource(new
    ByteArrayInputStream(xmlSting.getBytes())));

    Do I need to import any particular package?

    Thanking in Advance
    Sanjeev
     
    Sanjeev, May 2, 2008
    #2
    1. Advertising

  3. Sanjeev

    Filip Larsen Guest

    Sanjeev skrev:

    > SAXParser sp = spf.newSAXParser();
    > sp.parse("account.xml", this);
    > .....
    >
    > I am getting String from third party i.e. content of above xml
    > document/file in string.
    > my requirement is instead of reading from xml file, I need to read it
    > from String.


    You could use

    sp.parse(new InputSource(new StringReader(xmlString)), this);


    This is similar to what Timir suggests, but uses a character-based
    reader instead of converting all the way to bytes.


    Regards,
    --
    Filip Larsen
     
    Filip Larsen, May 2, 2008
    #3
  4. Sanjeev wrote:
    > But I am getting the foolowing error.
    > IFXParser.java:56: cannot find symbol
    > symbol : class InputSource
    > location: class IFXParser
    > sp.parse(new InputSource(new
    > ByteArrayInputStream(xmlSting.getBytes())));
    >
    > Do I need to import any particular package?



    org.xml.sax.InputSource
     
    Mike Schilling, May 3, 2008
    #4
  5. Fri, 2 May 2008 12:29:35 +0530, /Timir Hazarika/:
    > "Sanjeev" <> wrote in message
    > news:...
    >
    >> my requirement is instead of reading from xml file, I need to read it
    >> from String.

    >
    > Try this:
    >
    > sp.parse( new InputSource(new ByteArrayInputStream(string.getBytes())) );


    Using String.getBytes() will most probably fail if the string
    contains non-ASCII characters. String.getBytes() encodes the string
    using the platform's default encoding which is most often a
    single-byte one while the XML declaration in this case says the
    bytes have to be UTF-8. A lack of XML declaration will also force
    the parser to try UTF-8. If the input string contains characters
    which can't be represented in the platform's default encoding, they
    will be damaged already on String.getBytes(). On some Unix systems
    the platform's default encoding is US-ASCII which would damage all
    non-ASCII characters. Generally String.getBytes() should never be
    used for "real work", but getBytes(String charsetName) or
    getBytes(Charset).

    As there's no reason to add the overhead of encoding the readily
    decoded characters into bytes and then make the parser decode those
    bytes back into characters the best would be to pass the characters
    directly to the parser, as Filip Larsen has already suggested in
    another reply.

    --
    Stanimir
     
    Stanimir Stamenkov, May 4, 2008
    #5
    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. Replies:
    5
    Views:
    16,298
    Steve W. Jackson
    Sep 15, 2005
  2. vin b
    Replies:
    1
    Views:
    798
    Vityok
    Jan 6, 2006
  3. madan
    Replies:
    7
    Views:
    871
  4. Replies:
    2
    Views:
    955
    Joseph Kesselman
    Nov 5, 2007
  5. Erik Wasser
    Replies:
    5
    Views:
    495
    Peter J. Holzer
    Mar 5, 2006
Loading...

Share This Page