You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Savita S <sa...@india.hp.com> on 2002/04/10 12:40:50 UTC
HOW TO PARSE A XML STRING
Hi all,
How will I parse a xml String.
Thanks and regards
Savita.
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org
Re: HOW TO PARSE A XML STRING
Posted by Andy Clark <an...@apache.org>.
Jean-Charles DUFOUR wrote:
> I'm also very interesting about "how to parse a xml string", but I'm a
> newbie both with java and xerces (what a challenge!!!)
> May you please write the code sample to change the initial code produce by
> Gude Reshma and then show me how to < use the "getBytes(String)" > or <use
> a StringReader instead>
Sorry, I should have done this to begin with.
String XML_STRING = "<root/>";
InputStream method:
byte[] bytes = XML_STRING.getBytes("UTF8");
InputStream stream = new ByteArrayInputStream(bytes);
InputSource source = new InputSource(stream);
// parse document with input source
Reader method:
Reader reader = new StringReader(XML_STRING);
InputSource source = new InputSource(reader);
// parse document with input source
NOTE: You should always set the systemId of the input source
before parsing so that the parser is able to find resources
(like the DTD) that are relative to the input document. This
approach can be combined with a custom entity resolver to
parse the DTD from a String as well.
For example, first a custom entity resolver:
public class MyEntityResolver implements Entity Resolver {
public InputSource resolveEntity(String pubid, String sysid)
throws SAXException, IOException {
if (sysid.equals("http://example.com/xml/grammar.dtd")) {
String DTD_STRING = "<!ENTITY root EMPTY>";
StringReader reader = new StringReader(DTD_STRING);
InputSource source = new InputSource(reader);
source.setSystemId("http://example.com/xml/grammar.dtd");
return source;
}
return null;
}
}
Next, the parser that uses it:
String XML_STRING = "<!DOCTYPE root SYSTEM
'http://example.com/xml/grammar.dtd'>\n<root/>";
DOMParser parser = new DOMParser();
parser.setEntityResolver(new MyEntityResolver());
I'm instantiating an example parser directly but you can set
an entity resolver on the parsers returned by JAXP as well.
Hope this helps!
--
Andy Clark * andyc@apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org
RE: HOW TO PARSE A XML STRING
Posted by Jean-Charles DUFOUR <jc...@ifrance.com>.
Hello,
I'm also very interesting about "how to parse a xml string", but I'm a
newbie both with java and xerces (what a challenge!!!)
May you please write the code sample to change the initial code produce by
Gude Reshma and then show me how to < use the "getBytes(String)" > or <use
a StringReader instead>
Here is the initial code :
//######################################
public Document parseString(String str)
{
//System.out.println("in parse string :: string got is "+str);
Document doc = null;
try
{
byte[] byte_array_of_string = str.getBytes();
InputStream istr = new ByteArrayInputStream(byte_array_of_string);
InputSource is = new InputSource(istr);
DOMParser dom_parser = new DOMParser();
dom_parser.parse(is);
doc = dom_parser.getDocument();
}
catch(Exception e)
{
}
return(doc);
}
//##################################################"
Thanks you very much
Jean-Charles Dufour
-----Message d'origine-----
De : Andy Clark [mailto:andyc@apache.org]
Envoye : jeudi 11 avril 2002 15:49
A : xerces-j-user@xml.apache.org
Objet : Re: HOW TO PARSE A XML STRING
Gude Reshma wrote:
> byte[] byte_array_of_string = str.getBytes();
Never use the zero parameter "getBytes" method because it will
return the bytes in the system's default locale, which may not
be able to encode the characters in your file.
Instead, use the "getBytes(String)" variety so that you can
specify the encoding of the bytes that are returned. (Also
remember that you should match the encoding with what is
declared in the XMLDecl line -- <?xml version='1.0'
encoding='...'?>.)
Or better yet, use a StringReader instead. Then you don't have
to worry about matching the encoding specified in the XMLDecl.
--
Andy Clark * andyc@apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org
______________________________________________________________________________
ifrance.com, l'email gratuit le plus complet de l'Internet !
vos emails depuis un navigateur, en POP3, sur Minitel, sur le WAP...
http://www.ifrance.com/_reloc/email.emailif
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org
Re: HOW TO PARSE A XML STRING
Posted by Andy Clark <an...@apache.org>.
Gude Reshma wrote:
> byte[] byte_array_of_string = str.getBytes();
Never use the zero parameter "getBytes" method because it will
return the bytes in the system's default locale, which may not
be able to encode the characters in your file.
Instead, use the "getBytes(String)" variety so that you can
specify the encoding of the bytes that are returned. (Also
remember that you should match the encoding with what is
declared in the XMLDecl line -- <?xml version='1.0'
encoding='...'?>.)
Or better yet, use a StringReader instead. Then you don't have
to worry about matching the encoding specified in the XMLDecl.
--
Andy Clark * andyc@apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org
Re: HOW TO PARSE A XML STRING
Posted by Savita S <sa...@india.hp.com>.
hi ,
Can I parse a xml String which I am generating from a message Digest
using MD5 ALgorithm.
It is not able to parse the String giving an error something like :
An invalid XML character (Unicode: 0x7) was found in the element content
of the document.
Thanks and regards
Savita.
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org
Re: HOW TO PARSE A XML STRING
Posted by Gude Reshma <re...@yahoo.com>.
hi ,
use the following method
public Document parseString(String str)
{
//System.out.println("in parse string :: string got is "+str);
Document doc = null;
try
{
byte[] byte_array_of_string = str.getBytes();
InputStream istr = new ByteArrayInputStream(byte_array_of_string);
InputSource is = new InputSource(istr);
DOMParser dom_parser = new DOMParser();
dom_parser.parse(is);
doc = dom_parser.getDocument();
}
catch(Exception e)
{
}
return(doc);
}
Savita S <sa...@india.hp.com> wrote: Hi all,
How will I parse a xml String.
Thanks and regards
Savita.
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org
---------------------------------
Do You Yahoo!?
Yahoo! Tax Center - online filing with TurboTax