1.User.dtd Xml代码 1.<!ELEMENT User (name+,gender,birthday,address,job_time,curr_postion,email, 2.mobilephone+,curr_salary?,hope_salary,personal_price?)> 3.<!ATTLIST basic-information title CDATA #REQUIRED> 4.<!ELEMENT name (#PCDATA)> 5.<!ELEMENT gender EMPTY> 6.<!ATTLIST gender value (female|male) #REQUIRED> 7.<!ELEMENT birthday (#PCDATA)> 8.<!ELEMENT address (#PCDATA)> 9.<!ELEMENT job_time (#PCDATA)> 10.<!ELEMENT curr_postion (#PCDATA)> 11.<!ELEMENT email (#PCDATA)> 12.<!ELEMENT mobilephone (#PCDATA)> 13.<!ELEMENT curr_salary (#PCDATA)> 14.<!ELEMENT hope_salary (#PCDATA)> 15. 16.<!ELEMENT personal_price (#PCDATA)> 17.<!ENTITY personal "hello Daniel! You are a good Software Engineer!"> <!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代码 1.<?xml version="1.0" encoding="GBK"?> 2.<!DOCTYPE User SYSTEM "User.dtd"> 3.<User> 4. <name>Daniel Cheng</name> 5. <gender value="male"/> 6. <birthday>1985/01/08</birthday> 7. <address>北京市朝阳区东三环中路CBD</address> 8. <!--这个注释去掉的话,你将会发现DTD校验XML报告错误,因为DTD文件里根本没有定义hobby元素 9. <hobby>Swimming,Basketball!</hobby> 10. --> 11. <job_time>1</job_time> 12. <curr_postion>Software Engineer</curr_postion> 13. <email>mingdongcheng@163.com</email> 14. <mobilephone>15201010100</mobilephone> 15. <curr_salary/> 16. <hope_salary>10万-30万</hope_salary> 17. <personal_price> 18. 自我评价: 19. &personal; 20. </personal_price> 21.</User> <?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>mingdongcheng@163.com</email> <mobilephone>15201010100</mobilephone> <curr_salary/> <hope_salary>10万-30万</hope_salary> <personal_price> 自我评价: &personal; </personal_price> </User> 3.UserXMLValidateDTD.java Java代码 1.package exercise; 2. 3.import java.io.File; 4.import java.io.IOException; 5. 6.import javax.xml.parsers.DocumentBuilder; 7.import javax.xml.parsers.DocumentBuilderFactory; 8.import javax.xml.parsers.ParserConfigurationException; 9. 10.import org.xml.sax.SAXException; 11.import org.xml.sax.SAXParseException; 12.import org.xml.sax.helpers.DefaultHandler; 13. 14./** 15. * 用DTD规范校验XML文件内容的正确性 16. * @author Daniel Cheng 17. * 18. */ 19. 20.public class UserXMLValidateDTD { 21. private boolean validate = true; 22. 23. public UserXMLValidateDTD() { 24. 25. } 26. 27. public boolean XMLValidateDTD(File file) { 28. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 29. factory.setValidating(true); 30. try { 31. DocumentBuilder builder = factory.newDocumentBuilder(); 32. MyHandler mh = new MyHandler(); 33. builder.setErrorHandler(mh); 34. builder.parse(file); 35. } catch (ParserConfigurationException e) { 36. e.printStackTrace(); 37. } catch (SAXException e) { 38. e.printStackTrace(); 39. } catch (IOException e) { 40. e.printStackTrace(); 41. } 42. return validate; 43. } 44. 45. class MyHandler extends DefaultHandler { 46. String errorMessage = null; 47. 48. public void error(SAXParseException e) throws SAXException { 49. errorMessage = e.getMessage(); 50. System.out.println("一般错误!!!" + errorMessage); 51. validate = false; 52. } 53. 54. public void fatalError(SAXParseException e) throws SAXException { 55. errorMessage = e.getMessage(); 56. System.out.println("严重错误!!!" + errorMessage); 57. validate = false; 58. } 59. 60. } 61. 62.} 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 Java代码 1.package exercise; 2. 3.import java.io.File; 4.import java.io.IOException; 5.import java.util.Scanner; 6. 7.import javax.xml.parsers.DocumentBuilder; 8.import javax.xml.parsers.DocumentBuilderFactory; 9.import javax.xml.parsers.ParserConfigurationException; 10. 11.import org.w3c.dom.Attr; 12.import org.w3c.dom.Document; 13.import org.w3c.dom.Element; 14.import org.w3c.dom.NamedNodeMap; 15.import org.w3c.dom.Node; 16.import org.w3c.dom.NodeList; 17.import org.xml.sax.SAXException; 18./** 19. * 用DTD检验XML并解析 20. * @author Daniel Cheng 21. * 22. */ 23. 24.public class UserDomParser { 25. 26. public static void main(String[] args) throws ParserConfigurationException, 27. SAXException, IOException { 28. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 29. DocumentBuilder builder = factory.newDocumentBuilder(); 30. String fileName = ""; 31. File file = null; 32. Scanner sc = new Scanner(System.in); 33. while (true) { 34. if (fileName.equals("")) { 35. System.out.println("请输入xml文件名字:"); 36. fileName = sc.nextLine(); 37. } 38. file = new File(fileName); 39. if (!file.exists()) { 40. System.out.println("xml文件不存在!"); 41. fileName = ""; 42. continue; 43. } else { 44. break; 45. } 46. } 47. boolean validate = new UserXMLValidateDTD().XMLValidateDTD(file); 48. 49. if (validate) { 50. System.out.println("通过DTD校验!"); 51. Document doc = builder.parse(file); 52. Element root = doc.getDocumentElement(); 53. printElement(root); 54. } else { 55. System.out.println("该XML文件未通过DTD校验解析终止!!!"); 56. } 57. } 58. 59. public static void printElement(Element e) { 60. System.out.print("<" + e.getTagName()); 61. NamedNodeMap map = e.getAttributes(); 62. for (int i = 0; i < map.getLength(); i++) { 63. Attr attr = (Attr) map.item(i); 64. System.out.print(" " + attr.getName() + "=/"" + attr.getValue() 65. + "/""); 66. } 67. System.out.print(">"); 68. 69. NodeList list = e.getChildNodes(); 70. for (int i = 0; i < list.getLength(); i++) { 71. Node n = list.item(i); 72. if (n.getNodeType() == Node.ELEMENT_NODE) { 73. Element en = (Element) n; 74. printElement(en); 75. } else { 76. System.out.println(n.getTextContent()); 77. } 78. 79. } 80. System.out.print("</" + e.getTagName() + ">"); 81. } 82. 83.}