You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by Srikanth Sampath <sr...@sybase.com> on 2000/10/18 19:28:58 UTC
RESEND: How to retrieve the xml version string using SAX parser
Hi,
How do I retrieve the xml version header from an XML document, i.e.
the one <? xmlversion......?>. This is not considered a PI. Is there any
method which retrieves the header?. I am using xerces SAX parser.
Please help.
Thanks,
--Srikanth
Re: RESEND: How to retrieve the xml version string using SAX
parser
Posted by Patrick Haggerty <ha...@mindspring.com>.
Funny you should mention this. I was just reading the Java and XML book
from O'Reilly about just this thing. Their spin on it was that the version
of the XML document was specifically for the parser and was not for our
humble eyes.
Here is the way that I got around it. If you look at the documentation on
the SAXParser class you will find this wonderful method xmlDecl(int
versionIndex, int encodingIndex, int standaloneIndex)
Looking at the source code you can come up with a little work around. Just
override the method, get the info and let the parser do it's thing:
import org.apache.xerces.parsers.SAXParser;
public class MySAXParser extends SAXParser
{
public void xmlDecl(int versionIndex, int encodingIndex, int
standaloneIndex) throws Exception
{
String verInfo = "";
if (versionIndex != -1)//-1 if it is ommited
verInfo += " version='" +
fStringPool.toString(versionIndex) + "'";
if (encodingIndex != -1)
verInfo += " encoding='" +
fStringPool.toString(encodingIndex) + "'";
if (standaloneIndex != -1)
verInfo += " standalone='" +
fStringPool.toString(standaloneIndex) + "'";
System.out.println("Hey, I got that declaration after
all:<?xml" + verInfo + "?>");
super.xmlDecl(versionIndex, encodingIndex, standaloneIndex);
}
}
notice that the variable xmlInfo has all the information that you need.
Then in your parsing application, just change the line:
XMLReader parser =
XMLReaderFactory.createXMLReader(
"org.apache.xerces.parsers.SAXParser");
to use our new parser:
XMLReader parser =
XMLReaderFactory.createXMLReader(
"MySAXParser");
That should take care of you!
Take care and I will talk to you soon!!!
At 10:28 AM 10/18/2000 -0700, you wrote:
>Hi,
> How do I retrieve the xml version header from an XML document, i.e.
>the one <? xmlversion......?>. This is not considered a PI. Is there any
>method which retrieves the header?. I am using xerces SAX parser.
> Please help.
>
>Thanks,
>--Srikanth
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
>For additional commands, e-mail: xerces-j-dev-help@xml.apache.org
Cheers!
Hagg
That's Internet for:
Patrick E. Haggerty, III
Remember to be kind to rude people
They are just too stupid to know any better