开发者

XML validation using Java Code

开发者 https://www.devze.com 2023-01-22 07:43 出处:网络
I need some code sample which shows how I can validate a xml file against a schema. Below is my XML document:

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>
   开发者_如何学Go <year>1983</year>
</birthdate>

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>

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


You can try JDOM library.

http://www.jdom.org/docs/faq.html#a0360

0

精彩评论

暂无评论...
验证码 换一张
取 消