00 03Java Schema Web development of: XML-based alternative to DTD

1 Schema Introduction

dtd syntax: <ELEMENT element name constraint!>

in line with xml schema syntax, in line with xml syntax.

There can be more than an xml schema, only one dtd. Multiple schema namespaces to distinguish (similar to Java package name).

dtd which is only PCDATA type, but can support more data types in schema inside
| - such as age, can only be an integer, the schema can be directly define an integer type
therefore find schema syntax is more stringent.

schema syntax is more complex, schema currently can not replace dtd, the two coexist.

2 Schema development process

Create a schema file, the suffix name .xsd.
| - root <schema>
| - inside the element in the schema
| - (1) Properties: xmlns="http://www.w3.org/2001/XMLSchema"indicates the current file is a constraint file xml
| - (2) Properties: targetNamespace="http://www.itcast.cn/20151111"using file schema constraint, the constraint is introduced directly through the address file
| - (3) attributes: elementFormDefault="qualified"
step:
| - (1) to see how many elements in xml, to define how many <element>.
| - (2) look simple elements and complex elements. If the elements are complex, use <complexType>, and <sequence>the element is defined, for example:

<complexType>
    <sequence>
        子元素
    <sequencs>
</complexType>

If the element is a simple, direct use <element name="" type=""></element>format definition, refers to an attribute name element constraints file name, type refers to the data type.

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" 
targetNamespace="http://www.example.org/person" 
xmlns:tns="http://www.example.org/person" 
elementFormDefault="qualified">
<element name="person">
    <complexType>
        <sequence>
            <element name="name" type="string"></element>
            <element name="age" type="int" />
        </sequence>
    </complexType>
</element>

</schema>

| - (3) the introduction of constraint file in the file to be constrained inside, where xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"property indicates the file is to be constrained file xmlns="http://www.itcast.cn/20151111"attributes are bound documents in the targetNamespace attribute, xsi: schemaLocation = "http://www.itcast.cn/20151111 person.xsd "property refers to the location constraint file (format: targetNamespace 空格 约束文件地址路径)

<?xml version="1.0" encoding="UTF-8"?>
<person xmlns="http://www.example.org/person"
xmlns:per="http://www.w3.org/2001/XMLSchema-instance"
per:schemaLocation="http://www.example.org/person person.xsd"
>
    <name>lks</name>
    <age>23</age>
</person>

3 Schema constraints API View

(1) <sequence>: represent elements in order of appearance
(2) <all>: represents an element can appear only once
(3) <choice>: wherein a represents elements may occur
(4) maxOccurs="unbounded": attribute element, the element represents the number of occurrences
(4) <any></any>: represents any element

You can constrain property
(1) written in complex elements inside;
(2) written </complex>before,
(3) syntax: <attribute name="" type="" use=""></attribute>attribute name simply more constrained property name, type refers to whether the property type, use means the property must be present (required refers to must be present ).

Complex schema constraint
(1) the introduction of multiple schema files, you can give each individual file from the name.
(2) a plurality of redundant schema file constraints on the elements, may be used 别名:元素to define.

4 sax resolution process

Parse xml There are two techniques: dom and sax.

dom technical steps:
(1) a dispensing xml hierarchy tree structure in memory according to
(2) in the xml tags, attributes, text objects are packaged into

sax ways: event-driven, while being read resolution.

In java.xml.parsers package which:
(1) public abstract class SAXParser extends Object: Examples of such can () method from SAXParserFactory.newSAXParser. Using the public void parse​(File f, DefaultHandler dh) throws SAXException, IOExceptionmethod, the first parameter is xml path, a second parameter is the event handler.
(2) public abstract class SAXParserFactory extends Object: Example () obtained by newInsance.

sax implementation process:

5 sax manner of use Jaxp

sax way can not be achieved additions and deletions can be done only for query operation

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<person>
    <p1>
        <name>lks</name>
        <age>23</age>
    </p1>
    <p1>
        <name>hhy</name>
        <age>20</age>
    </p1>
</person>
public class JaxpTest {

	public static void main(String []args) throws Exception{
		SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
		SAXParser saxParser = saxParserFactory.newSAXParser();
		saxParser.parse("src/dtdlearn/mydtd00/person.xml", new MyDefaultHandler());
	}
}

Print out the entire document.
(1) Creating a parser facility
(2) to create a parser
(3) performs the parse method
(4) themselves create a class that inherits DefaultHandler
three methods (5) inside the rewritable type

class MyDefaultHandler extends DefaultHandler{

	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		// TODO Auto-generated method stub
		System.out.print("<" + qName + ">");
	}

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		// TODO Auto-generated method stub
		System.out.print("<\\" + qName + ">");
	}

	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		// TODO Auto-generated method stub
		System.out.print(new String(ch, start, length));
	}
	
}

Gets the value of all the name elements.
(1) defines a member variable flag = to false
(2) determines the start whether the method name element, if the name element, the flag = to true
(. 3) is determined flag whether true, and if so, prints out character () method of the content
( 4) when the execution to the end of the name, set flag is false

class MyDefaultHandler1 extends MyDefaultHandler{
	private boolean flag = false;

	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		// TODO Auto-generated method stub
		if( "name".equals(qName)) {
			flag = true;
		}
	}
	
	

	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		// TODO Auto-generated method stub
		if(flag== true) {
			super.characters(ch, start, length);
			System.out.println();
		}
	}



	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		// TODO Auto-generated method stub
		if( "name".equals(qName)) {
			flag = false;
		}
	}
} 

Get the first name value element.
(1) defining the member variable idx = 1
(2) determines the start whether the method is name, if it is, continues to determine idx is set, and if so, then flag = to true
(. 3) is determined flag whether true, if so, printing content
(4) when the end of the element name to execute, flag = false

class MyDefaultHandler2 extends MyDefaultHandler1{
	private int idx = 1;

	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		// TODO Auto-generated method stub
		super.startElement(uri, localName, qName, attributes);
	}

	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		// TODO Auto-generated method stub
		if( idx == 1) {
			super.characters(ch, start, length);
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		// TODO Auto-generated method stub
		super.endElement(uri, localName, qName);
		if("name".equals(qName)) {
			idx++;
		}
	}
}
Published 77 original articles · won praise 11 · views 2634

Guess you like

Origin blog.csdn.net/weixin_43762330/article/details/104532645
DTD