<, >, &
</description>
CDATA XML- ( ), ' < ', ' > ', ' & ', ' ' ' ', , CDATA. CDATA " <[CDATA[ ", " ]]> ", . CDATA , , : <data><[CDATA[ 5 < 7 ]]></data>XML- :
(well-formed): XML;
(valid): , ; XML. XML DTD XML- XSD , .
DTD
XML- DTD (Document Type Definition). DTD XSD. DTD , XML , , (upgrade).
DTD , () XML-, (, , <student> <name>, <telephone> <address>), .
, , .
DTD XML- , XML DTD, (validness) XML-, .
DTD, , DTD- XML.
XML DTD:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><! DOCTYPE students SYSTEM "students.dtd">XML-:
<?xml version="1.0"?>
<! DOCTYPE student [<!ELEMENT student (name, telephone, address)> <!-- name, telephone, address --> ]> DTD !ELEMENT, . , <student>, <name>, <telephone> <address>, :<!ELEMENT name (#PCDATA)>
<!ELEMENT telephone (#PCDATA)>
<!ELEMENT address (country, city, street)>
|
|
: name, telephone address PCDATA. , , - (PCDATA parsed character data). EMPTY ANY .
<student>, , <name>, <telephone> <address>. + ( ), * (0 ), ? (0 1), . , ,
<!ELEMENT student (name, telephone, address)> , student name, telephone address. , | (). : <!ELEMENT student (#PCDATA | body)> student body, PCDATA.!ATTLIST, , , : <!ATTLIST _ _ _ __ > :
<!ATTLIST student
login ID #REQUIRED
faculty CDATA #REQUIRED>
<student> : login, faculty. , :
CDATA ;
ID ;
IDREF (IDREFS) ( ), ;
ENTITY (ENTITIES) ( , );
NMTOKEN (NMTOKENS) ( , ).
. :
#REQUIRED , ;
#IMPLIED , , , .
#FIXED , , , DTD.
defaultValue , . #FIXED, defaultValue.
, DTD. .
(entity) , . !ENTITY:
<!ENTITY company 'Sun Microsystems'><sender>&company;</sender>-, , Sun Microsystems &company.
DTD () .
<!ENTITY % elementGroup firstName, lastName,gender, address, phone>
|
|
<!ELEMENT employee (%elementGroup;)>
<!ELEMENT contact (%elementGroup)>
XML . , , :
<!ENTITY logotype SYSTEM "/image.gif" NDATA GIF87A>DTD students.xml :
<?xml version='1.0' encoding='UTF-8'?>
<!ELEMENT students (student)+>
<!ELEMENT student (name, telephone, address)>
<!ATTLIST student
login ID #REQUIRED
faculty CDATA #REQUIRED
>
<!ELEMENT name (#PCDATA)>
<!ELEMENT telephone (#PCDATA)>
<!ELEMENT address (country, city, street)>
<!ELEMENT country (#PCDATA)>
<!ELEMENT city (#PCDATA)>
<!ELEMENT street (#PCDATA)>
XSD
XSD , DTD, XML-. XSD-, DTD, XML- , , . DTD, (44 ) (namespace). XSD .
XSD XML
<?xml version="1.0" encoding="UTF-8"?>schema.
: , , . element, :
ref , ;
name ;
type ;
minOccurs maxOccurs ( 1), , , maxOccurs unbounded.
, . . , , .
, , .
, string ( ), boolean ( ), integer ( ), float ( ), ID () . simpleType. name .
, name cc . restriction. base . restriction :
minInclusive , ;
maxInclusive ;
length ;
pattern ;
enumeration .
Login, ID pattern.
<simpleType name="Login">
<restriction base="ID">
<pattern value="[a-zA-Z]{3}[a-zA-Z0-9_]+"/>
</restriction>
</simpleType>
, / , .
complexType. , name . , , sequence. element, . , , mixed true. , ,
attribute. attribute: name , type . , , use, required, optional, prohibited. default, fixed.
|
|
Student:
<complexType name="Student">
<sequence>
<element name="name" type="string"/>
<element name="telephone" type="decimal"/>
<element name="address" type="tns:Address"/>
</sequence>
<attribute name="login" type="tns:Login"
use="required"/>
<attribute name="faculty" type="string"
use="required"/>
</complexType>
, , simpleContent extension, ().
<element name="Student">
<complexType>
<simpleContent>
<extension base="string">
<attribute name="birthday" type="string"/>
</extension>
</simpleContent>
</complexType>
</element>
/ complexContent.
<complexType name=personType>
<sequence>
<element name=firstName type=string/>
<element name=lastName type=string/>
<element name=address type=string/>
</sequence>
</complexType>
<complexType name=studentType>
<complexContent>
<extension base=personType>
<sequence>
<element name=course type=integer/>
<element name=faculty type=string/>
</sequence>
</extesion>
</complexContent>
</complexType>
<element name=Student type=studentType/>
XML , <all>, .
<element name=person>
<complexType>
<all>
<element name=firstName type=string/>
<element name=lastName type=string/>
</all>
</complexType>
</element>
<choice> , XML . <sequence> .
XML- students.xsd :
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.com/Students"
xmlns:tns="http://www.example.com/Students">
<element name="students">
<complexType>
<sequence>
<element name="student" type="tns:Student" minOccurs="1" maxOccurs="unbounded" />
</sequence>
</complexType>
</element>
<complexType name="Student">
<sequence>
<element name="name" type="string" />
<element name="telephone" type="decimal" />
<element name="address" type="tns:Address" />
|
|
</sequence>
<attribute name="login" type="tns:Login" use="required" />
<attribute name="faculty" type="string" use="required" />
</complexType>
<simpleType name="Login">
<restriction base="ID">
<pattern value="[a-zA-Z]{3}[a-zA-Z0-9_]*"/>
</restriction>
</simpleType>
<complexType name="Address">
<sequence>
<element name="country" type="string" />
<element name="city" type="string" />
<element name="street" type="string" />
</sequence>
</complexType>
</schema>
namespace. , . xmlns , .
, xmlns="http://www.w3.org/2001/XMLSchema" , , "http://www.w3.org/2001/XMLSchema" .
targetNamespace="http://www.example.com/Students" /, .
xmlns:tns="http://www.example.com/Students" () . , , tns, tns:Address, tns:Login ..
, , .
- DTD XSD, XML- DTD <students> <tns:students> :
<tns:students xmlns:tns="http://www.example.com/Students"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.com/Students students.xsd ">
Java.
/* # 13: XML: XSDMain.java */
package chapt16.xsd;
import java.io.IOException;
import org.xml.sax.SAXException;
import org.apache.xerces.parsers.DOMParser;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import chapt16.xsd.MyErrorHandler;
public class XSDMain {
public static void main(String[] args) {
String filename = "students.xml";
DOMParser parser = new DOMParser();
try {
//
parser.setErrorHandler(new MyErrorHandler("log.txt"));
// XSD
parser.setFeature(
"http://xml.org/sax/features/validation", true);
parser.setFeature(
"http://apache.org/xml/features/validation/schema", true);
parser.parse(filename);
} catch (SAXNotRecognizedException e) {
e.printStackTrace();
System. out. print(" ");
} catch (SAXNotSupportedException e) {
e.printStackTrace();
System. out. print(" ");
} catch (SAXException e) {
e.printStackTrace();
System. out. print(" SAX ");
} catch (IOException e) {
e.printStackTrace();
System. out. print(" I/O ");
}
System. out. print(" " + filename + " ");
}
}
:
/* # 14: : MyErrorHandler.java */package chapt16.xsd;
import java.io.IOException;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXParseException;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.SimpleLayout;
public class MyErrorHandler implements ErrorHandler {
private Logger logger;
public MyErrorHandler(String log) throws IOException {
// chapt16.xsd
logger = Logger.getLogger("chapt16.xsd");
//
logger.addAppender(new FileAppender(
new SimpleLayout(), log));
}
public void warning(SAXParseException e) {
|
|
logger.warn(getLineAddress(e) + "-" +
e.getMessage());
}
public void error(SAXParseException e) {
logger.error(getLineAddress(e) + " - "
+ e.getMessage());
}
public void fatalError(SAXParseException e) {
logger.fatal(getLineAddress(e) + " - "
+ e.getMessage());
}
private String getLineAddress(SAXParseException e) {
//
return e.getLineNumber() + ": "
+ e.getColumnNumber();
}
}
, XML- . , login. :
ERROR - 14: 41 - cvc-id.2: There are multiple occurrences of ID value 'mit'.
ERROR - 14: 41 - cvc-attribute.3: The value 'mit' of attribute 'login' on element 'student' is not valid with respect to its type, 'login'.
XML-, , telephone, :
FATAL - 7: 26 - Element type "telephone2456474" must be followed by either attribute specifications, ">" or "/>".
Java XML.
Java 6, JDK.
XML .
/* # 15: XML- : DemoJSR.java */package chapt16;
import java.io.*;
import javax.xml.bind.*;
import javax.xml.bind.annotation.*;
public class DemoJSR {
public static void main(String[] args) {
try {
JAXBContext context =
JAXBContext. newInstance (Student. class);
Marshaller m = context.createMarshaller();
Student s = new Student(1, "Bender"); //
m.marshal(s, new FileOutputStream("stud.xml"));
} catch (FileNotFoundException e) {
System. out. println("XMl- ");
e.printStackTrace();
} catch (JAXBException e) {
System. out. println("JAXB-");
e.printStackTrace();
}
}
@XmlRootElement
private static class Student { //
private int id;
private String name;
public Student() {
}
public Student(int id, String name) {
this. id = id;
this. name = name;
}
public int getID() {
return id;
}
public String getName() {
return name;
}
public void setID(int id) {
this. id = id;
}
public void setName(String name) {
this. name = name;
}
}
}
XML-:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <student>
<ID>1</ID>
<name>Bender</name>
</student>
XML- Java:
/* # 16: University, Course Faculty XSD-: student.xsd*/<schema xmlns="http://www.w3c.org/2001/XMLSchema"
xmlns:Revealed="http://www.university.net"
targetNamespace="http://www.university.net">
<element name="University">
<complexType>
<sequence>
<element name="faculty" type="Revealed:Faculty"/>
<element name="course" type="Revealed:Course"/>
</sequence>
</complexType>
</element>
<complexType name="Course">
<sequence>
<element name="login" type="string"/>
<element name="name" type="string"/>
<element name="telephone" type="string"/>
</sequence>
</complexType>
<simpleType name="Faculty">
<restriction base="string">
<enumeration value="FPMI"></enumeration>
<enumeration value="MMF"></enumeration>
<enumeration value="Geo"></enumeration>
</restriction>
</simpleType>
</schema>
:
Xjc student.xsd
:
package net.university;
import javax.xml.bind.annotation.XmlEnum;
import javax.xml.bind.annotation.XmlEnumValue;
@XmlEnum
public enum Faculty {
FPMI ("FPMI"),
MMF ("MMF"),
@XmlEnumValue("Geo")
GEO_F ("Geo");
private final String value;
Faculty(String v) {
value = v;
}
public String value() {
return value;
}
public static Faculty fromValue(String v) {
for (Faculty c: Faculty. values ()) {
if (c.value.equals(v)) {
return c;
}
}
throw new IllegalArgumentException(v.toString());
}
}
package net.university;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
/**
* <p>Java class for Course complex type.
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Course", propOrder = {
"login",
"name",
"telephone"
})
public class Course {
@XmlElement(required = true)
protected String login;
@XmlElement(required = true)
protected String name;
@XmlElement(required = true)
protected String telephone;
public String getLogin() {
return login;
}
public void setLogin(String value) {
this. login = value;
}
public String getName() {
return name;
}
public void setName(String value) {
this. name = value;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String value) {
this. telephone = value;
}
}
package net.university;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
/**
* <p>Java class for anonymous complex type.
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"faculty",
"course"
})
@XmlRootElement(name = "University")
public class University {
@XmlElement(required = true)
protected Faculty faculty;
@XmlElement(required = true)
protected Course course;
public Faculty getFaculty() {
return faculty;
}
public void setFaculty(Faculty value) {
this. faculty = value;
}
public Course getCourse() {
return course;
}
public void setCourse(Course value) {
this. course = value;
}
}
package net.university;
import javax.xml.bind.annotation.XmlRegistry;
@XmlRegistry
public class ObjectFactory {
public ObjectFactory() {
}
public Course createCourse() {
return new Course();
}
public University createUniversity() {
return new University();
}
}
XML-
XML , , . , . ML , XML- ().
( Java: ), XML-, .