XML validation using Java Code(使用 Java 代码进行 XML 验证)
问题描述
我需要一些代码示例来展示如何根据架构验证 xml 文件.下面是我的 XML 文档:
I need some code sample which shows how I can validate a xml file against a schema. Below is my XML document:
<birthdate>
<month>January</month>
<day>21</day>
<year>1983</year>
</birthdate>
我要验证上述 XML 的架构是:
The schema against which I want to validate the above XML is:
<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 文档按照我指定的模式有效,则该代码将这些作为输入并给出正确的输出?
Now can some one help me write the Java code that will take these as input and give proper output if the XML doc is a valid as per the schema I specified?
现在我在理解下面的代码时遇到了问题,比如 MySAXHandler 上的方法是如何被调用的,因为类没有被实例化并且方法没有被显式调用.谁能解释一下?还有有什么方法可以直接传递文件而不是通过字符串传递.
Now i have issue understanding the below code like how the methods on MySAXHandler are getting calling becoz the class is not instantiated and methods are not called explicitely. Can anyone explain? And also is there any way i can pass the files direcly instead of passing through Strings.
代码是 -
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(true);
try {
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 = "<?xml version="1.0"?>" +
"<birthdate>" +
"<month>January</month>" +
"<day>21</day>" +
"<year>1983</year>" +
"</birthdate>";
static String schemaString ="<?xml version="1.0" encoding="UTF-8"?>" +
"<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>";
}
class MySAXHandler extends 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();
if(attrCount>0) {
System.out.println("Attributes:");
for(int i = 0 ; i<attrCount ; i++) {
System.out.println(" Name : " + attr.getQName(i));
System.out.println(" Type : " + attr.getType(i));
System.out.println(" Value: " + attr.getValue(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库.
You can try JDOM library.
http://www.jdom.org/docs/faq.html#a0360
这篇关于使用 Java 代码进行 XML 验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 Java 代码进行 XML 验证
基础教程推荐
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 降序排序:Java Map 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01