Xml Java DOM解析器验证不需要';行不通
我正在尝试使用DOM解析器验证xml。由于某些原因,解析器无法识别指定的命名空间。有什么问题吗 它抛出错误:Xml Java DOM解析器验证不需要';行不通,java,xml,Java,Xml,我正在尝试使用DOM解析器验证xml。由于某些原因,解析器无法识别指定的命名空间。有什么问题吗 它抛出错误: Error: URI=null Line=5: cvc-elt.1: Cannot find the declaration of element 'ioc'. 守则: String outputString = "<?xml version=\"1.0\" encoding=\"us-ascii\"?>\n" + "<ioc\n" + "xmlns:
Error: URI=null Line=5: cvc-elt.1: Cannot find the declaration of element 'ioc'.
守则:
String outputString = "<?xml version=\"1.0\" encoding=\"us-ascii\"?>\n" +
"<ioc\n" +
"xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'\n" +
"xmlns:xsd='http://www.w3.org/2001/XMLSchema'\n" +
"xmlns=\"http://schemas.mandiant.com/2010/ioc\" >\n" +
"</ioc>";
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
factory.setNamespaceAware(true);
factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
"http://www.w3.org/2001/XMLSchema");
DocumentBuilder builder = factory.newDocumentBuilder();
builder.parse(new ByteArrayInputStream(outputString.getBytes("UTF-8")));
String outputString=“\n”+
“\n”+
"";
DocumentBuilderFactory工厂=DocumentBuilderFactory.newInstance();
工厂设置验证(true);
factory.setNamespaceAware(true);
factory.setAttribute(“http://java.sun.com/xml/jaxp/properties/schemaLanguage",
"http://www.w3.org/2001/XMLSchema");
DocumentBuilder=factory.newDocumentBuilder();
parse(新的ByteArrayInputStream(outputString.getBytes(“UTF-8”));
解决方案是添加schemaLocation属性和所有必需的依赖项
xsi:schemaLocation="http://schemas.mandiant.com/2010/ioc
http://schemas.mandiant.com/2010/ioc/ioc.xsd
http://schemas.mandiant.com/2010/ioc/TR/
http://schemas.mandiant.com/2010/ioc/TR/ioc-TR.xsd"
我不能说您提供的实现有什么问题,但这里有一个方法,我用来根据模式验证XML文档(仅使用标准库)
我认为您没有设置模式,验证是正确的,但是模式在哪里?我认为它应该使用
xmlns=”指定的模式http://schemas.mandiant.com/2010/ioc“< /代码> NoPE,是用于与模式匹配但没有自动下载模式的XML命名空间,请考虑离线Surnaly。如果要验证xml,需要加载模式并将其设置到Factory中,则需要添加一个属性xsi:schemaLocation=”http://schemas.mandiant.com/2010/ioc http://example.com/schema.xsd“
我认为应该向解析器指示模式位置。当然,它可以是文件:/dir/subdir/schema.xsd
而不是HTTP URL。xsi:schemaLocation
属性值应该是URL对的列表,其中每对URL的第一个值都是命名空间的URL(在您的情况下是http://schemas.mandiant.com/2010/ioc
)第二个URL为该名称空间提供模式的位置,因此xsi:schemaLocation=”http://schemas.mandiant.com/2010/ioc http://schemas.mandiant.com/2010/ioc/ioc.xsd“
是一个有意义的例子。只有一个URL没有意义。
import java.io.File;
import java.io.IOException;
import javax.xml.XMLConstants;
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;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
/**
*
* Purpose: validate an XML document against a schema
* @param dom The org.w3c.dom.Document object representing the XML document
* @param xsdPath The path to the XML schema file
* @return True if the XML instance validates against the schema.
*/
private static boolean validateXML(Document dom, String xsdPath){
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Source schemaFile = new StreamSource(new File(xsdPath));
try {
Schema schema = factory.newSchema(schemaFile);
Validator validator = schema.newValidator();
// validate the DOM tree
validator.validate(new DOMSource(dom));
} catch (SAXException se) {
se.printStackTrace();
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
return true;
}