在vs2010中,写了一个xml文件,xsd文件,如果写的xml文件不符合xsd,怎么验证它的错误,vs会自动报错吗?
3个回答
展开全部
这有个.java的验证方法
修改相应路径后可直接运行验证
package test;
import java.io.File;
import javax.xml.XMLConstants;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
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;
public class TestDOMValidation
{
public static void main(String[] args)
{
try
{
// Get Document Builder Factory
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// Leave off validation, and turn off namespaces
factory.setValidating(false);
factory.setNamespaceAware(false);
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new File("data/test/shiporder.xml"));
//Document doc = builder.parse(new File("data/test/simple.xml"));
// SchemaFactory is a schema compiler. It reads external representations of schemas and
// prepares them for validation.
SchemaFactory constraintFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
// Source: an object that implements this interface contains the information needed to act
// as source input (XML source or transformation instructions).
Source constraints = new StreamSource(new File("data/test/shiporder.xsd"));
//Source constraints = new StreamSource(new File("data/test/simple.xsd"));
// Schema object represents a set of constraints that can be checked/ enforced against an
// XML document.
Schema schema = constraintFactory.newSchema(constraints);
// Validator is a processor that checks an XML document against Schema.
Validator validator = schema.newValidator();
// Validate the DOM tree
try
{
validator.validate(new DOMSource(doc));
System.out.println("Document validates fine.");
}
catch (org.xml.sax.SAXException e)
{
System.out.println("Validation error: " + e.getMessage());
}
}
catch (ParserConfigurationException e)
{
System.out.println("The underlying parser does not support the requested features.");
}
catch (FactoryConfigurationError e)
{
System.out.println("Error occurred obtaining Document Builder Factory.");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
修改相应路径后可直接运行验证
package test;
import java.io.File;
import javax.xml.XMLConstants;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
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;
public class TestDOMValidation
{
public static void main(String[] args)
{
try
{
// Get Document Builder Factory
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// Leave off validation, and turn off namespaces
factory.setValidating(false);
factory.setNamespaceAware(false);
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new File("data/test/shiporder.xml"));
//Document doc = builder.parse(new File("data/test/simple.xml"));
// SchemaFactory is a schema compiler. It reads external representations of schemas and
// prepares them for validation.
SchemaFactory constraintFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
// Source: an object that implements this interface contains the information needed to act
// as source input (XML source or transformation instructions).
Source constraints = new StreamSource(new File("data/test/shiporder.xsd"));
//Source constraints = new StreamSource(new File("data/test/simple.xsd"));
// Schema object represents a set of constraints that can be checked/ enforced against an
// XML document.
Schema schema = constraintFactory.newSchema(constraints);
// Validator is a processor that checks an XML document against Schema.
Validator validator = schema.newValidator();
// Validate the DOM tree
try
{
validator.validate(new DOMSource(doc));
System.out.println("Document validates fine.");
}
catch (org.xml.sax.SAXException e)
{
System.out.println("Validation error: " + e.getMessage());
}
}
catch (ParserConfigurationException e)
{
System.out.println("The underlying parser does not support the requested features.");
}
catch (FactoryConfigurationError e)
{
System.out.println("Error occurred obtaining Document Builder Factory.");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
参考资料: 这是我们老师布置的作业里面的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这已经不是VS的问题了,是自己的函数问题。
具体的话网上有很多开源的代码,小文件用DOM,10M以上的用SAX。
打开文件成功后,无报错证明XML语法通过。
然后使用开源代码验证是否符合xsd。
详阅:http://www.vckbase.com/document/viewdoc/?id=911
具体的话网上有很多开源的代码,小文件用DOM,10M以上的用SAX。
打开文件成功后,无报错证明XML语法通过。
然后使用开源代码验证是否符合xsd。
详阅:http://www.vckbase.com/document/viewdoc/?id=911
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
vs有一个验证函数。自己百度。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询