Ich denke, „die wirklich hängt“, hängt davon ab, wie Sie Codierung und Weise, die Sie verweisen, also würde ich hervorheben beide Ansätze:
Ansatz # 1: Übergeben Sie als Direktstrom
Sie können die XSD direkt als Eingabestream an Ihr Programm übergeben, so dass Sie nicht auf das Attribut "noNamespaceSchemaLocation" angewiesen sind, auch wenn Sie keine Namespaces für Elemente in Ihrem XML-Dokument haben.
Im Folgenden finden Sie ein Beispielprogramm:
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.InputSource;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
public class XmlSchemaValidationHelper {
public static void main(String[] argv) {
XmlSchemaValidationHelper schemaValidationHelper = new XmlSchemaValidationHelper();
schemaValidationHelper.validateAgainstSchema(new File(argv[0]), new File(argv[1]));
}
public void validateAgainstSchema(File xmlFile, File xsdFile) {
try {
System.out.println("### Starting...");
// parse an XML document into a DOM tree
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
builderFactory.setNamespaceAware(true);
DocumentBuilder parser = builderFactory.newDocumentBuilder();
Document document = parser.parse(xmlFile);
// create a SchemaFactory capable of understanding WXS schemas
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
// load a WXS schema, represented by a Schema instance
Source schemaFile = new StreamSource(xsdFile);
Schema schema = factory.newSchema(schemaFile);
// create a Validator instance, which can be used to validate an
// instance document
Validator validator = schema.newValidator();
// validate the DOM tree
validator.validate(new DOMSource(document));
System.out.println("### Finished...");
} catch (FileNotFoundException ex) {
throw new OpenClinicaSystemException("File was not found", ex.getCause());
} catch (IOException ioe) {
throw new OpenClinicaSystemException("IO Exception", ioe.getCause());
} catch (SAXParseException spe) {
spe.printStackTrace();
throw new OpenClinicaSystemException("Line : " + spe.getLineNumber() + " - " + spe.getMessage(), spe.getCause());
} catch (SAXException e) {
throw new OpenClinicaSystemException(e.getMessage(), e.getCause());
} catch (ParserConfigurationException pce) {
throw new OpenClinicaSystemException(pce.getMessage(), pce.getCause());
}
}
public class OpenClinicaSystemException extends RuntimeException {
/**
*
*/
private static final long serialVersionUID = 1L;
private String errorCode;
private Object[] errorParams;
public OpenClinicaSystemException(String code, String message) {
this(message);
this.errorCode = code;
}
public OpenClinicaSystemException(String code, String message, Throwable cause) {
this(message, cause);
this.errorCode = code;
}
public OpenClinicaSystemException(String message, Throwable cause) {
super(message, cause);
}
public OpenClinicaSystemException(Throwable cause) {
super(cause);
}
public OpenClinicaSystemException(String message) {
super(message);
this.errorCode = message;
}
public OpenClinicaSystemException(String code, Object[] errorParams) {
this.errorCode = code;
this.errorParams = errorParams;
}
public OpenClinicaSystemException(String code, Object[] errorParams, String message) {
this(message);
this.errorCode = code;
this.errorParams = errorParams;
}
public String getErrorCode() {
return errorCode;
}
public Object[] getErrorParams() {
return errorParams;
}
public void setErrorParams(Object[] errorParams) {
this.errorParams = errorParams;
}
}
}
Run es wie folgt aus: E:\xmlValidator>java XmlSchemaValidationHelper po.xml test/po.xsd
und Sie müssen Attribut nicht, hängt von der „noNamespaceSchemaLocation“, weil Sie Ihren Validator direkt die XSD als Eingangsstrom sind vorbei/Parser.
Dies ist die schnelle und schmutzige Möglichkeit, XMLs gegen XSD zu validieren.
Ansatz # 2: Entfliehen Sie dem XSD Pfad richtig
Sie müssen nur richtig Ihre XSD Weg zu entkommen, so verwenden entweder von unten statt:
<Module2Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file:///C://project//module2//src//main//java//com//test//Modules.xsd">
ODER
<Module2Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C://project//module2//src//main//java//com//test//Modules.xsd">
lesen MSDN official article ungefähr gleich.
Abschließende Worte: Verwenden Sie XML-Namespaces und target
Ich würde empfehlen, dass Sie XML-Namespace und target für richtig Zusammenhang die XML-Instanzen mit XML-Schema verwenden sollten. Betrachten Sie unter Beispiel XSD und XML.
Für alle, die die Konzepte des XML-Namespaces und target verstehen nicht, ich hoffe, unter schnellen Punkten hilfreich wäre:
- Im XSD, müssen Sie die „target“ wie diese
targetNamespace="http://www.books.org"
definieren die ist nichts anderes als Sie definieren den Namespace für Ihre tatsächliche XML-Instanz. Und derselbe Namespace, d. H. http://www.books.org
müssen Sie in Ihrer XML-Instanz verwenden.
- In der XML-Instanz, müssen Sie: vom XSD als Standard-Namespace
- die „target“ Machen Sie ähnliche
xmlns="http://www.books.org"
- Und dann verwenden „schema“ -Attribut den XML-Validator zu sagen, dass das, was sollte kann die Ziel-Namensraum im XML-Schema und Validator wird derefence die entsprechend
xsi:schemaLocation="http://www.books.org"
XML-Beispiel URI:
<?xml version="1.0"?>
<BookStore xmlns="http://www.books.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.books.org">
<Book>
<Title>My Life and Times</Title>
<Author>Paul McCartney</Author>
<Date>1998</Date>
<ISBN>1-56592-235-2</ISBN>
<Publisher>McMillin Publishing</Publisher>
</Book>
<Book>
<Title>Illusions The Adventures of a Reluctant Messiah</Title>
<Author>Richard Bach</Author>
<Date>1977</Date>
<ISBN>0-440-34319-4</ISBN>
<Publisher>Dell Publishing Co.</Publisher>
</Book>
<Book>
<Title>The First and Last Freedom</Title>
<Author>J. Krishnamurti</Author>
<Date>1954</Date>
<ISBN>0-06-064831-7</ISBN>
<Publisher>Harper & Row</Publisher>
</Book>
</BookStore>
Beispiel XSD:
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.books.org"
xmlns="http://www.books.org"
elementFormDefault="qualified">
<xsd:simpleType name="ISBN-type">
<xsd:restriction base="xsd:string">
<xsd:pattern value="\d{1}-\d{5}-\d{3}-\d{1}|\d{1}-\d{3}-\d{5}-\d{1}|\d{1}-\d{2}-\d{6}-\d{1}"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:element name="BookStore">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Book" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="Author" type="xsd:string"/>
<xsd:element name="Date" type="xsd:gYear"/>
<xsd:element name="ISBN" type="ISBN-type"/>
<xsd:element name="Publisher" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Haben Sie versucht, 'xsi: noNamespaceSchemaLocation =" file: /// C:/Projekt/module2/src/main/java/com/test/Module.xsd "'? –
Ja, aber ich möchte nicht den relativen Pfad mit dem vollständigen Dateipfad zur XSD-Datei verwenden – bontade