如何在xml文件中调用外部xsd?

时间:2012-06-07 06:53:34

标签: java xml xsd

大家好,这是我的xml文件,xsd定义为内部

<?xml version="1.0"?>
<catalog xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
         xmlns:x="urn:book"> 
<!-- START OF SCHEMA -->
<xsd:schema targetNamespace="urn:book">
 <xsd:element name="book">
  <xsd:complexType>
    <xsd:sequence>
      <xsd:element name="author" type="xsd:string"/>
      <xsd:element name="title" type="xsd:string"/>
      <xsd:element name="genre" type="xsd:string"/>
      <xsd:element name="price" type="xsd:float"/>
      <xsd:element name="publish_date" type="xsd:date"/>
      <xsd:element name="description" type="xsd:string"/>
      <xsd:element name="number" type="xsd:integer"/>
    </xsd:sequence>
    <xsd:attribute name="id" type="xsd:string"/>
  </xsd:complexType>
 </xsd:element>
</xsd:schema>
<!-- END OF SCHEMA -->
   <x:book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications with
      XML.</description>
      <number>123.4 </number>
   </x:book>
</catalog>

我想在java中验证它,我已经编写了下面的java代码,它对这个xml非常有效。

import java.io.*;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import javax.xml.validation.*;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
import javax.xml.xpath.*;
import org.xml.sax.InputSource;

public class TestValidation {
    public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException, XPathExpressionException {
        XPath xpath = XPathFactory.newInstance().newXPath();
        NodeList nodes = (NodeList)xpath.evaluate("/*/*", new InputSource("E:\\abc.xml"), XPathConstants.NODESET);
        SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
        Validator validator = factory.newSchema(new DOMSource(nodes.item(0))).newValidator();
        try {
            validator.validate(new DOMSource(nodes.item(1)));
            System.out.println("XML is valid.");
        }
        catch (SAXException ex) {
            System.out.println("XML is not valid because " + ex.getMessage());
        }
    }
}

现在我想将xsd定义为一个单独的外部文件,我知道如何定义它,任何人都可以帮我找到如何从这个xml调用xsd然后我需要在我的java中进行任何更改程序

1 个答案:

答案 0 :(得分:0)

我假设您正在关注http://msdn.microsoft.com/en-us/library/windows/desktop/ms759142%28v=vs.85%29.aspx中的示例。您可以使用其external-namespaced.xml和books2.xsd示例。

要验证XML,请使用此处的答案:

Validate XML against multiple arbitrary schemas

或更改您的代码以直接加载架构

  Validator validator = factory.newSchema(new StreamSource(
      TestValidation.class.getResourceAsStream("/books2.xsd"))).newValidator();
  try {
     validator.validate(new DOMSource(nodes.item(0)));
     System.out.println("XML is valid.");
  } catch (SAXException ex) {
     System.out.println("XML is not valid because " + ex.getMessage());
  }