Home  >  Article  >  Backend Development  >  XML concise tutorial (7)

XML concise tutorial (7)

黄舟
黄舟Original
2017-02-18 15:39:281529browse

Table of Contents


Development History

##XMLComparison with HTML Extensible

XML Syntax details compared with HTML

XML validation DTD

XMLNamespace

XMLSyntax structure

XML Validation Schema

DOM4JRead and write configuration file

About SLT

XML Validation Schema



##As mentioned in the previous article Yes, through DTD we can easily judge whether the XML to be verified conforms to the specifications we have defined (the relationship between elements, whether the values ​​of attributes are correct), but if we want to verify the content of elements, DTD is powerless, so people study A new verification method - Schema.

In addition to the above advantages,

Schema is even more impressive than DTD The exciting thing is that it is itself a well-formed XML document, so it is very easy to write Schema. Compared with DTD which has its own independent syntax, it is very difficult to write and maintain. A Schema file is an XML file, so the corresponding

Schema

## written by XML The process of # is to write XML against XML. In this case, it is very easy to write Schema. The following demonstrates how to write the corresponding Schema against XMLOriginal XML file (test2.xml)


 

 George Bush
 
  John Adams
  
Oxford Street
London UK
Empire Burlesque Special Edition 1 10.90 Hide your heart 1 9.90




##For the above XML Let’s start creating a Schema. The principle to follow is how to write the original XML and then describe the corresponding Schema, just like you are face to face with a person. The description is the same.

Schema code is as follows (shiporder.xsd)

##

 


 
  
   
   
    
     
      
      
      
      
     
    
   
   
    
     
      
      
      
      
     
    
   
  
  
 

 


Code analysis:

The first line is all

XML The statement needs no elaboration.

The second line does this

XML(Schema itself is a XML) defines a namespace.

Starting from the fourth line are some requirements for the original

XML:

首先定义了根元素为shiporder(行4),其次因为shiporder元素有一个属性,其中包含其他的元素所以其为复合类型(行5)。然后通过sequence元素按照顺序包围其子元素(行10---15),描述元素的名称以及元素的类型(行11----14),如果需要描述元素的限制条件(行22)。描述根元素的属性,由于是必选属性所以选择required关键字,需要注意的是这个属性必须放在最后(行29

通过Schema验证XML的代码和前面文章中的DTD验证大同小异,代码如下:

package ValidateXml;
 
import java.io.File;
import java.io.IOException;
 
import javax.xml.XMLConstants;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
 
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
 
importcom.sun.org.apache.xml.internal.utils.DefaultErrorHandler;
 
public class XmlValidator
{
    private String xsdFilePath;
 
    public XmlValidator(String xsdFilePath)
    {
        this.xsdFilePath =xsdFilePath;
    }
 
    public String validata(String xmlFilePath,ErrorHandler errorHandler)
    {
        String msg = null;
        SchemaFactoryfactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        try
        {
           Schema schema = factory.newSchema(new File(xsdFilePath));
           Validator validator = schema.newValidator();
           validator.setErrorHandler(errorHandler);
           validator.validate(new StreamSource(new File(xmlFilePath)));
        }
        catch (SAXExceptione)
        {
           msg = e.getMessage();
           e.printStackTrace();
        }
        catch (IOExceptione)
        {
           msg = e.getMessage();
           e.printStackTrace();
        }
        return msg;
    }
 
    public static void main(String[] args)
    {
        String xmlFilePath ="d://test2.xml";
        String xsdFilePath ="d://shiporder.xsd";
        XmlValidator my =new XmlValidator(xsdFilePath);
        String msg =my.validata(xmlFilePath, new DefaultErrorHandler());
       System.out.println(msg == null);
    }
}

如果原XML文件符合Schema文件中的描述则返回true;否则抛出异常进行描述哪里不符合,并且返回false。(具体的操作可在实际工程中自行定制,这里只是进行简单的描述)



 以上就是XML简明教程(7) 的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:XML concise tutorial (6)Next article:XML concise tutorial (6)