我有来自 String 和 Document
中的 Web 服务的 SOAP 响应格式,我有一个验证它的方法。问题是我必须验证节点 <Result>
.
我已经得到了这个节点,但不知道如何获取带有标签等的子节点
Node result = (Node)xPath.compile("//Result").evaluate(xmlDocument, XPathConstants.NODE);
<Result>
<playerID>some id</playerID>
<partnerUID>some partner uid</partnerUID>
<registrationLevel>some registration level</registrationLevel>
<properties>
<property>
<key>some key</key>
<value>some value</value>
</property>
<property>...</property>
</Result>
感谢帮助
最佳答案
我使用这种方式针对 XSD 验证 XML 文件。我认为这会对您有所帮助。
public String validateXMLSchema() throws SAXException, IOException
{
File folder = new File("xsdPath");
File[] listOfFiles = folder.listFiles();
String tempXsdFile;
for( int i=0; i < listOfFiles.length; i++ )
{
if(listOfFiles[i].isFile())
{
tempXsdFile = listOfFiles[i].getName();
try
{
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(new File(xsdPath + tempXsdFile));
Validator validator = schema.newValidator();
validator.validate(new StreamSource(new File("C://Users//test.xml")));
return tempXsdFile;
}
catch (IOException | SAXException e)
{
System.out.println("ERROR: XML not well known"+ e.getMessage());
}
}
}
return null;
}
关于 java 。 XML。如何根据 XSD 架构验证 xml 文档的某些部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22931422/