67,512
社区成员
发帖
与我相关
我的任务
分享
<?xml version="1.0" encoding="UTF-8"?>
<birthdate>
<month>January</month>
<day>21</day>
<year>1983</year>
<name>aaa</name>
</birthdate>
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<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:any minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
public class XMLParse {
public static void main(String[] args)
throws SAXException, IOException, ParserConfigurationException {
SchemaFactory factory
= SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
File schemaLocation = new File("D:\\tmp\\test.xsd");
File xmlFile = new File("D:\\tmp\\test.xml");
Schema schema = factory.newSchema(schemaLocation);
Validator validator = schema.newValidator();
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse(xmlFile);
DOMSource source = new DOMSource(doc);
DOMResult result = new DOMResult();
try {
validator.validate(source, result);
Document augmented = (Document) result.getNode();
}
catch (SAXException ex) {
System.out.println(ex.getMessage());
}
}
}