使用Java代码进行XML验证

我需要一些代码示例,说明如何根据模式验证xml文件。 以下是我的XML文档:

<birthdate>
    <month>January</month>
    <day>21</day>
    <year>1983</year>
</birthdate>

我想验证上述XML的模式是:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:import namespace="http://www.w3.org/XML/1998/namespace"
        schemaLocation="http://www.w3.org/2001/xml.xsd" />

  <xs:element name="birthdate">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="month" type="xs:string" />
        <xs:element name="day" type="xs:int" />
        <xs:element name="year" type="xs:int" />
      </xs:sequence>  
    </xs:complexType>
  </xs:element>
</xs:schema>

现在有人能帮我编写将这些作为输入的Java代码,并且如果根据我指定的模式,XML文档是有效的,那么可以输出正确的输出?

现在我有问题了解下面的代码,比如MySAXHandler上的方法如何调用,因为类没有实例化,并且方法没有被明确地调用。 谁能解释一下? 还有什么方法可以直接传递文件而不是通过字符串传递。

代码是 -

import java.io.StringReader;

import javax.xml.XMLConstants; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import javax.xml.transform.sax.SAXSource; import javax.xml.validation.SchemaFactory;

import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import org.xml.sax.helpers.DefaultHandler;

public class XMLval {public static void main(String args [])throws Exception {SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser parser = null; spf.setNamespaceAware(真); 尝试{SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); spf.setSchema(sf.newSchema(new SAXSource(new InputSource(new StringReader(schemaString)))));

 parser = spf.newSAXParser();
}
catch(SAXException e) {
  e.printStackTrace(System.err);
  System.exit(1);    
} 
catch(ParserConfigurationException e) {
  e.printStackTrace(System.err);
  System.exit(1);    
}
MySAXHandler handler = new MySAXHandler(); 
System.out.println(schemaString);
parser.parse(new InputSource(new StringReader(xmlString)), handler);

} static String xmlString =“”+“”+“January”+“21”+“1983”+“”;

static String schemaString =“”+

  "<xs:element name="birthdate">" +
  "<xs:complexType>" +
  "<xs:sequence>" +
  "<xs:element name="month" type="xs:string"/>" +
  "<xs:element name="day" type="xs:int"/>" +
  "<xs:element name="year" type="xs:int" />" +
  "</xs:sequence>" +
                  "</xs:complexType>" +
                  "</xs:element>" +
                  "</xs:schema>";

}

类MySAXHandler扩展了DefaultHandler {public void startDocument(){System.out.println(“Start document:”); }
public void endDocument(){System.out.println(“End document:”); }

public void startElement(String uri,String localName,String qname,Attributes attr){System.out.println(“Start element:local name:”+ localName +“qname:”+ qname +“uri:”+ uri); int attrCount = attr.getLength(); 如果(attrCount> 0){System.out.println(“Attributes:”); for(int i = 0; i

public void endElement(String uri,String localName,String qname){System.out.println(“End element:local name:”+ localName +“qname:”+ qname +“uri:”+ uri); }

public void characters(char [] ch,int start,int length){System.out.println(“Characters:”+ new String(ch,start,length)); }}


你可以尝试JDOM库。

http://www.jdom.org/docs/faq.html#a0360

链接地址: http://www.djcxy.com/p/34933.html

上一篇: XML validation using Java Code

下一篇: Is there a difference between "throw" and "throw ex"?