java如何验证xml,使用Java代码进行XML验证

I need some code sample which shows how I can validate a xml file against a schema. Below is my XML document:

January

21

1983

The schema against which I want to validate the above XML is:

schemaLocation="http://www.w3.org/2001/xml.xsd" />

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?

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.

Code is -

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\"?>" +

"" +

"January" +

"21" +

"1983" +

"";

static String schemaString ="<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +

"" +

"" +

"" +

"" +

"" +

"" +

"" +

"" +

"" +

"";

}

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

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));

}

}

解决方案

You can try JDOM library.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值