久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用 Java 代碼進行 XML 驗證

XML validation using Java Code(使用 Java 代碼進行 XML 驗證)
本文介紹了使用 Java 代碼進行 XML 驗證的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我需要一些代碼示例來展示如何根據(jù)架構(gòu)驗證 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 的架構(gòu)是:

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>

現(xiàn)在有人可以幫我編寫 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?

現(xiàn)在我在理解下面的代碼時遇到了問題,比如 MySAXHandler 上的方法是如何被調(diào)用的,因為類沒有被實例化并且方法沒有被顯式調(diào)用.誰能解釋一下?還有有什么方法可以直接傳遞文件而不是通過字符串傳遞.

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

這篇關(guān)于使用 Java 代碼進行 XML 驗證的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環(huán)繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數(shù)據(jù)庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 亚洲人成人网 | wwwxxx日本在线观看 | 国产精品美女久久久久久免费 | 婷婷久久五月 | 久久久久久久久久久久亚洲 | 一区二区三区国产精品 | 国产91丝袜在线熟 | 日韩精品一区二区三区视频播放 | 亚洲色图婷婷 | 久久久国产精品一区 | 国产日韩精品一区 | 毛片一级片 | 亚洲综合无码一区二区 | 欧美黑人一级爽快片淫片高清 | 国产精品麻 | 偷拍第一页| 羞羞视频网站免费看 | 国产在线不卡 | 午夜电影网 | 男女一区二区三区 | 亚洲播放一区 | 亚洲激情综合网 | 狠狠干av | 一本色道精品久久一区二区三区 | 久久久久国产视频 | 一区二区三区在线电影 | 一区二区欧美在线 | 欧美精品中文字幕久久二区 | 综合久久综合久久 | 一级在线免费观看 | 欧美中文字幕一区二区 | 欧美亚洲国产一区 | 欧美一区二区免费 | 中文字幕 在线观看 | 黄色片免费在线观看 | av一区二区三区四区 | 欧美综合视频 | 久草免费在线 | 国产一区二区在线视频 | 成人在线视频网站 | 色约约视频 |