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

S

Sanjeev

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
 
S

Sanjeev

Try this:

sp.parse(  new InputSource(new ByteArrayInputStream(string.getBytes()))  );

- Timir











- 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
 
F

Filip Larsen

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,
 
M

Mike Schilling

Sanjeev said:
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
 
S

Stanimir Stamenkov

Fri, 2 May 2008 12:29:35 +0530, /Timir Hazarika/:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,224
Latest member
BettieToom

Latest Threads

Top