Thread View: pl.comp.xml
1 messages
1 total messages
Started by abc_javy
Mon, 08 Sep 2008 12:44
validacja XML by XSD a problem namespace w SAXParser
Author: 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";
}
}
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads