XMLValidator - PLEASE HELP!!! [message #177217] |
Thu, 17 August 2006 12:04  |
Eclipse User |
|
|
|
Hello everyone! I'm having a problem. I'm using the XMLValidator to try to
validate an xml file using a dtd. I created a small xml file with a dtd
file. I noticed that if the xml tags are broken (example: misspelled), i
get a null pointer exception when processing the xml file and have no good
info on where it broke, like line number, etc. Is there a different tool
to use for xml syntax or am i just totally confused? From you experience,
how should i handle this? Here is the java code, xml file and dtd file.
Thanks much!
Jason
XMLValidator validator = new XMLValidator();
InputStream iostream = null;
String xmlFile = "C:\\xml\\people.xml";
String dtdFile = "C:\\xml\\people.dtd";
try {
iostream = new BufferedInputStream(new FileInputStream(new
File(xmlFile)));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
XMLValidationReport report = null;
try {
report = validator.validate(dtdFile, iostream);
} catch (Exception e) {
System.out.println(e);
}
System.out.println(report);
String fileURI = report.getFileURI();
System.out.println(fileURI);
for (ValidationMessage message : report.getValidationMessages()) {
System.out.println("\t"+message);
}
}
xml file --
<!DOCTYPE com.xstream.Person-array SYSTEM "file:/C:/xml/people.dtd">
<com.xstream.Person-array>
<com.xstream.Person>
<name>Jason</name>
<age>26</age>
<children/>
</com.xstream.Peson> <!-- this Peson should be Person, this is the
error I'm trying to find -->
<com.xstream.Person>
<name>Donna</name>
<age>62</age>
<children>
<com.xstream.Person>
<name>Jason</name>
<age>26</age>
<children/>
</com.xstream.Person>
</children>
</com.xstream.Person>
</com.xstream.Person-array>
dtd file
<!DOCTYPE com.xstream.Person-array SYSTEM "file:/C:/xml/people.dtd">
<com.xstream.Person-array>
<com.xstream.Person>
<name>Jason</name>
<age>26</age>
<children/>
</com.xstream.Peson>
<com.xstream.Person>
<name>Donna</name>
<age>62</age>
<children>
<com.xstream.Person>
<name>Jason</name>
<age>26</age>
<children/>
</com.xstream.Person>
</children>
</com.xstream.Person>
</com.xstream.Person-array>
|
|
|
|
|
Re: XMLValidator - PLEASE HELP!!! [message #177272 is a reply to message #177264] |
Thu, 17 August 2006 18:05  |
Eclipse User |
|
|
|
This is a multi-part message in MIME format.
------=_NextPart_000_0016_01C6C21F.4DE5D340
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Thanks Lawrence.....
Well, now using that "file:///c:\\" format for the dtdFile i'm getting =
syntax errors on hte report.getValidationMessages() but it's not =
validating against the dtd. I removed the DTD from the top of my xml =
file so my xml file will be independent of the dtd. Correct me if I'm =
wrong, but in the call "report =3D validator.validate()" method, the =
first param is the URI so "file:///C:\\blah\\file.dtd" in a string =
format, and then the second param is an input stream which is an input =
for the xml file, right? so like validate(string file path to dtd, =
input stream for xml file)? I don't want the DTD to be referenced in =
the xml file becuase i'm using XStream to create the xml file from a =
java object. Please advise.
--xml file
<com.xstream.Person-array>
<com.xstream.Person>
<name>Jason</name>
<qage>26</qage> <-- Bad Tag, should be age -->
<children />
</com.xstream.Person>
<com.xstream.Person>
<name>Donna</name>
<age>62</age>
<children>
<com.xstream.Person>
<name>Jason</name>
<age>26</age>
<children />
</com.xstream.Person>
</children>
</com.xstream.Person>
</com.xstream.Person-array>
--DTD
<!ELEMENT age (#PCDATA)>
<!ELEMENT com.xstream.Person-array (#PCDATA | com.xstream.Person)*>
<!ELEMENT name (#PCDATA)>
<!ELEMENT children (#PCDATA | com.xstream.Person)*>
<!ELEMENT com.xstream.Person (#PCDATA | name | age | children)*>
Thanks,
Jason
"Ed Merks" <merks@ca.ibm.com> wrote in message =
news:ec2ks3$p3e$1@utils.eclipse.org...
Lawrence,
I would highly recommend never using a "\" within a URI. It's really =
not the same as a "/" and is treated just like a plain old character =
when it occurs in a URI. This might not become evident until a relative =
path is resolved against it...
lmandel@ca.ibm.com wrote:=20
The XML validator expects a valid URI representing the file location.=20
Specify a valid URL to the files such as
String xmlFile =3D "file:///C:\\xml\\people.xml";
String dtdFile =3D "file:///C:\\xml\\people.dtd";
and I think your NPE will dissapear.
Note: If you're interested in XML validation outside of Eclipse you may=20
want to take a look at Apache Xerces [1]. The WTP XML validator reuses=20
Xerces under the covers.
Lawrence
[1]http://xerces.apache.org
=20
------=_NextPart_000_0016_01C6C21F.4DE5D340
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type =
content=3Dtext/html;charset=3DISO-8859-1>
<META content=3D"MSHTML 6.00.2900.2668" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY text=3D#000000 bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>Thanks Lawrence.....</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2>Well, now using that "<A=20
href=3D"file:///c:\\">file:///c:\\</A>" format for the dtdFile i'm =
getting syntax=20
errors on hte report.getValidationMessages() but it's not =
validating=20
against the dtd. I removed the DTD from the top of my xml file so =
my xml=20
file will be independent of the dtd. Correct me if I'm wrong, but =
in the=20
call "report =3D validator.validate()" method, the first param is the =
URI so "<A=20
href=3D"file:///C:\\blah\\file.dtd">file:///C:\\blah\\file.dtd</A>" in a =
string=20
format, and then the second param is an input stream which is an input =
for the=20
xml file, right? so like validate(string file path to dtd, input =
stream=20
for xml file)? I don't want the DTD to be referenced in the xml =
file=20
becuase i'm using XStream to create the xml file from a java =
object. =20
Please advise.</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2>--xml=20
file<BR><com.xstream.Person-array><BR> <com.xstream.Person&=
gt;<BR> =20
<name>Jason</name><BR> =20
<qage>26</qage> &nb sp; &nb s=
p; =20
<-- Bad Tag, should be age --><BR> <children=20
/><BR> </com.xstream.Person> <BR> <com.xstream.Perso=
n><BR> =20
<name>Donna</name><BR> =
<age>62</age><BR> =20
<children><BR> =20
<com.xstream.Person><BR> =20
<name>Jason</name><BR> =20
<age>26</age><BR> <children=20
/><BR> </com.xstream.Person><BR> =20
</children><BR> </com.xstream.Person> <BR></com.xstrea=
m.Person-array></FONT></DIV>
<DIV> </DIV><FONT face=3DArial size=3D2>
<DIV><BR>--DTD</DIV>
<DIV> </DIV>
<DIV><!ELEMENT age (#PCDATA)><BR><!ELEMENT =
com.xstream.Person-array=20
(#PCDATA | com.xstream.Person)*><BR><!ELEMENT name=20
(#PCDATA)><BR><!ELEMENT children (#PCDATA |=20
com.xstream.Person)*><BR><!ELEMENT com.xstream.Person (#PCDATA | =
name |=20
age | children)*></DIV>
<DIV> </DIV>
<DIV>Thanks,<BR>Jason</FONT></DIV>
<BLOCKQUOTE=20
style=3D"PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; =
BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
<DIV>"Ed Merks" <<A =
href=3D"mailto:merks@ca.ibm.com">merks@ca.ibm.com</A>>=20
wrote in message <A=20
=
href=3D"news:ec2ks3$p3e$1@utils.eclipse.org">news:ec2ks3$p3e$1@utils.ecli=
pse.org</A>...</DIV>Lawrence,<BR><BR>I=20
would highly recommend <B>never </B>using a "\" within a URI. =
It's=20
really not the same as a "/" and is treated just like a plain old =
character=20
when it occurs in a URI. This might not become evident until a =
relative=20
path is resolved against it...<BR><BR><BR><A =
class=3Dmoz-txt-link-abbreviated=20
href=3D"mailto:lmandel@ca.ibm.com">lmandel@ca.ibm.com</A> wrote:=20
<BLOCKQUOTE cite=3Dmidec2k0e$rfp$4@utils.eclipse.org =
type=3D"cite"><PRE wrap=3D"">The XML validator expects a valid URI =
representing the file location.=20
Specify a valid URL to the files such as
String xmlFile =3D <A class=3Dmoz-txt-link-rfc2396E =
href=3D"file:///C:\\xml\\people.xml">"file:///C:\\xml\\people.xml"</A>;
String dtdFile =3D <A class=3Dmoz-txt-link-rfc2396E =
href=3D"file:///C:\\xml\\people.dtd">"file:///C:\\xml\\people.dtd"</A>;
and I think your NPE will dissapear.
Note: If you're interested in XML validation outside of Eclipse you may=20
want to take a look at Apache Xerces [1]. The WTP XML validator reuses=20
Xerces under the covers.
Lawrence
[1]<A class=3Dmoz-txt-link-freetext =
href=3D"http://xerces.apache.org">http://xerces.apache.org</A>
</PRE></BLOCKQUOTE><BR></BLOCKQUOTE></BODY></HTML>
------=_NextPart_000_0016_01C6C21F.4DE5D340--
|
|
|
Powered by
FUDForum. Page generated in 0.05021 seconds