1.User.dtd
<!ELEMENT User (name+,gender,birthday,address,job_time,curr_postion,email, mobilephone+,curr_salary?,hope_salary,personal_price?)> <!ATTLIST basic-information title CDATA #REQUIRED> <!ELEMENT name (#PCDATA)> <!ELEMENT gender EMPTY> <!ATTLIST gender value (female|male) #REQUIRED> <!ELEMENT birthday (#PCDATA)> <!ELEMENT address (#PCDATA)> <!ELEMENT job_time (#PCDATA)> <!ELEMENT curr_postion (#PCDATA)> <!ELEMENT email (#PCDATA)> <!ELEMENT mobilephone (#PCDATA)> <!ELEMENT curr_salary (#PCDATA)> <!ELEMENT hope_salary (#PCDATA)> <!ELEMENT personal_price (#PCDATA)> <!ENTITY personal "hello Daniel! You are a good Software Engineer!">
2.User.xml
<?xml version="1.0" encoding="GBK"?> <!DOCTYPE User SYSTEM "User.dtd"> <User> <name>Daniel Cheng</name> <gender value="male"/> <birthday>1985/01/08</birthday> <address>北京市朝阳区东三环中路CBD</address> <!--这个注释去掉的话,你将会发现DTD校验XML报告错误,因为DTD文件里根本没有定义hobby元素 <hobby>Swimming,Basketball!</hobby> --> <job_time>1</job_time> <curr_postion>Software Engineer</curr_postion> <email>[email protected]</email> <mobilephone>15201010100</mobilephone> <curr_salary/> <hope_salary>10万-30万</hope_salary> <personal_price> 自我评价: &personal; </personal_price> </User>
3.UserXMLValidateDTD.java
package exercise; import java.io.File; import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import org.xml.sax.helpers.DefaultHandler; /** * 用DTD规范校验XML文件内容的正确性 * @author Daniel Cheng * */ public class UserXMLValidateDTD { private boolean validate = true; public UserXMLValidateDTD() { } public boolean XMLValidateDTD(File file) { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true); try { DocumentBuilder builder = factory.newDocumentBuilder(); MyHandler mh = new MyHandler(); builder.setErrorHandler(mh); builder.parse(file); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return validate; } class MyHandler extends DefaultHandler { String errorMessage = null; public void error(SAXParseException e) throws SAXException { errorMessage = e.getMessage(); System.out.println("一般错误!!!" + errorMessage); validate = false; } public void fatalError(SAXParseException e) throws SAXException { errorMessage = e.getMessage(); System.out.println("严重错误!!!" + errorMessage); validate = false; } } }
4.UserDomParser.java
package exercise; import java.io.File; import java.io.IOException; import java.util.Scanner; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Attr; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; /** * 用DTD检验XML并解析 * @author Daniel Cheng * */ public class UserDomParser { public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); String fileName = ""; File file = null; Scanner sc = new Scanner(System.in); while (true) { if (fileName.equals("")) { System.out.println("请输入xml文件名字:"); fileName = sc.nextLine(); } file = new File(fileName); if (!file.exists()) { System.out.println("xml文件不存在!"); fileName = ""; continue; } else { break; } } boolean validate = new UserXMLValidateDTD().XMLValidateDTD(file); if (validate) { System.out.println("通过DTD校验!"); Document doc = builder.parse(file); Element root = doc.getDocumentElement(); printElement(root); } else { System.out.println("该XML文件未通过DTD校验解析终止!!!"); } } public static void printElement(Element e) { System.out.print("<" + e.getTagName()); NamedNodeMap map = e.getAttributes(); for (int i = 0; i < map.getLength(); i++) { Attr attr = (Attr) map.item(i); System.out.print(" " + attr.getName() + "=\"" + attr.getValue() + "\""); } System.out.print(">"); NodeList list = e.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { Node n = list.item(i); if (n.getNodeType() == Node.ELEMENT_NODE) { Element en = (Element) n; printElement(en); } else { System.out.println(n.getTextContent()); } } System.out.print("</" + e.getTagName() + ">"); } }