Article View: pl.comp.xml
Article #9974validacja XML by XSD a problem namespace w SAXParser
From: abc_javy
Date: Mon, 08 Sep 2008 12:44
Date: Mon, 08 Sep 2008 12:44
151 lines
5496 bytes
5496 bytes
X-No-Archive: Yes
ja mam blad czy ministerstwo ?
Witam, napisalem sobie klase
i niestety gdy probuje validowac, a dokladniej
000001005_COIA_Spis.xml z tej stronki za pomoca
http://epl.icm.edu.pl/viewtopic.php?t=126&highlight=spis
====================================
<?xml version="1.0" encoding="UTF-8" ?>
<SpisZdawczoOdbiorczy xmlns="http://www.mswia.gov.pl/standardy/ndap"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mswia.gov.pl/standardy/ndap
SpisZdawczo-odbiorczy.xsd">
<Sprawa>.....
......
====================================
https://ndapapp.nask.net.pl/pack/SpisZdawczo-odbiorczy.xsd
====================================
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.mswia.gov.pl/standardy/ndap"
xmlns:tns="http://www.mswia.gov.pl/standardy/ndap"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<element name="SpisZdawczoOdbiorczy">
<complexType>.........
.......
====================================
to wywala wyjatkiem
Error on line 2: cvc-elt.1: Cannot find the declaration of element
'SpisZdawczoOdbiorczy'.
obojetnie czy dac w metodzie Saxa
saxBuilder.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",
new File(pathfilexsd).toURI().toString());
czy
saxBuilder.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation",
new File(pathfilexsd).toURI().toString());
Klase dolaczam, mozna sobie xml i xsd sicagnac i klase wywolac
ValidatorXML validatorXML = new ValidatorXML();
validatorXML.validate("./SpisZdawczo-odbiorczy.xsd",
"./000001005_COIA_Spis.xml",
false);
Jakiekolwiek pomysly mile widziane, bo po tygodniu nie ma juz zadnych
-------------klasa------
package com.ja.xml;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
/**
* @author ja
*
*/
public class ValidatorXML {
public String validate(String pathfilexsd, String pathfilexml, boolean
nonamespace)
{
System.out.println(new File(pathfilexml).toURI().toString());
org.jdom.Document myDocument = null;
SAXBuilder builder = new SAXBuilder();
try {
myDocument = builder.build(new FileInputStream(pathfilexml));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return validate(pathfilexsd, myDocument, nonamespace);
}
public String validate(String pathfilexsd, org.jdom.Document myDocument,
boolean nonamespace)
{
String DEFAULT_PARSER_NAME =
"org.apache.xerces.parsers.SAXParser";
SAXBuilder saxBuilder = new SAXBuilder(DEFAULT_PARSER_NAME, true);
// Turn on validation
saxBuilder.setFeature("http://apache.org/xml/features/validation/schema",
true);
saxBuilder.setFeature("http://apache.org/xml/features/validation/schema-full-checking",
true);
saxBuilder.setFeature("http://xml.org/sax/features/validation", true);
File filexsd = new File(pathfilexsd);
if (!filexsd.exists())
{
System.err.println(new Exception());
return "Brak pliku xsd";
}
if (nonamespace)
{
saxBuilder.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",
new File(pathfilexsd).toURI().toString());
}
else
{
saxBuilder.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation",
new File(pathfilexsd).toURI().toString());
}
System.out.println(new File(pathfilexsd).toURI().toString());
ByteArrayOutputStream out = new ByteArrayOutputStream();
XMLOutputter serializer = new XMLOutputter(Format.getCompactFormat());
try {
serializer.output(myDocument, out);
} catch (IOException e) {
e.printStackTrace();
return e.getMessage();
}
//
try {
ByteArrayInputStream byteArrayInputStream = new
ByteArrayInputStream(out.toByteArray());
saxBuilder.build(byteArrayInputStream);
} catch (JDOMException e) {
System.err.println(e.getMessage());
return e.getMessage();
} catch (IOException e) {
e.printStackTrace();
return e.getMessage();
}
System.out.println("Validacja zakońcozna pomyślnie");
return "0";
}
}
Technical Details
Message-ID:
<ga36np$ahr$2@inews.gazeta.pl>Path:
mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.gazeta.pl!not-for-mailShow Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.gazeta.pl!not-for-mail From: "abc_javy" <bez@spamu.com> Newsgroups: pl.comp.lang.java,pl.comp.xml Subject: validacja XML by XSD a problem namespace w SAXParser Date: Mon, 8 Sep 2008 14:44:43 +0200 Organization: "Portal Gazeta.pl -> http://www.gazeta.pl" Lines: 157 Message-ID: <ga36np$ahr$2@inews.gazeta.pl> NNTP-Posting-Host: ika210.internetdsl.tpnet.pl X-Trace: inews.gazeta.pl 1220877881 10811 79.190.0.210 (8 Sep 2008 12:44:41 GMT) X-Complaints-To: usenet@agora.pl NNTP-Posting-Date: Mon, 8 Sep 2008 12:44:41 +0000 (UTC) X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3350 X-Priority: 3 X-RFC2646: Format=Flowed; Original X-Newsreader: Microsoft Outlook Express 6.00.2900.3138 X-User: mkmkasdf X-MSMail-Priority: Normal MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit