1.\u754c\u9762\u4ecb\u7ecd<\/h2>\n
<\/p>\n
2.\u7cfb\u7edf\u7ed3\u6784\u4ecb\u7ecd<\/h2>\n
<\/p>\n
\u6211\u628a\u5404\u4e2a\u5305\u4ecb\u7ecd\u4e00\u4e0b<\/strong><\/p>\n 1.exam.xml\u6587\u4ef6\u5c31\u662f\u7528\u6765\u6a21\u62df\u6570\u636e\u5e93\u7684\uff0c\u4ee3\u7801\u5728\u540e\u9762\u8d34\u51fa\u6765<\/strong><\/p>\n 2.dao\u5c42\u65f6\u64cd\u4f5c\u6570\u636e\u5305<\/strong><\/p>\n 3.utils\u662f\u5de5\u5177\u7c7b\u5305<\/strong><\/p>\n 4.bean\u662f\u6570\u636e\u5c01\u88c5\u7c7b\u5305<\/strong><\/p>\n 5.exception\u662f\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\u5305<\/strong><\/p>\n 6.junit\u6d4b\u8bd5\u5305<\/strong><\/p>\n 7.ui\u7528\u6237\u754c\u9762\u5305<\/strong><\/p>\n 1.exam.xml<\/strong><\/p>\n 2.XmlUtils.java<\/strong><\/p>\n getDocument()\u65b9\u6cd5\u7528\u6765\u83b7\u53d6\u6807\u7b7e\u5bf9\u8c61<\/strong><\/p>\n write2Xml(Document document)\u65b9\u6cd5\u7528\u6765\u66f4\u65b0xml\u6587\u4ef6<\/strong><\/p>\n 3.StudentDao.java<\/strong><\/p>\n3.\u8be6\u7ec6\u4ee3\u7801<\/strong><\/h2>\n
1<\/span> <?<\/span>xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"<\/span>?><\/span><<\/span>exam<\/span>><\/span> 2<\/span> <<\/span>student <\/span>examid<\/span>=\"456\"<\/span> idcard<\/span>=\"123\"<\/span>><\/span> 3<\/span> <<\/span>name<\/span>><\/span>a<\/<\/span>name<\/span>><\/span> 4<\/span> <<\/span>location<\/span>><\/span>b<\/<\/span>location<\/span>><\/span> 5<\/span> <<\/span>grade<\/span>><\/span>22<\/<\/span>grade<\/span>><\/span> 6<\/span> <\/<\/span>student<\/span>><\/span> 7<\/span> 8<\/span> <<\/span>student <\/span>examid<\/span>=\"000\"<\/span> idcard<\/span>=\"789\"<\/span>><\/span> 9<\/span> <<\/span>name<\/span>><\/span>c<\/<\/span>name<\/span>><\/span> 10<\/span> <<\/span>location<\/span>><\/span>d<\/<\/span>location<\/span>><\/span> 11<\/span> <<\/span>grade<\/span>><\/span>20<\/<\/span>grade<\/span>><\/span> 12<\/span> <\/<\/span>student<\/span>><\/span> 13<\/span> 14<\/span> 15<\/span> <\/<\/span>exam<\/span>><\/span><\/pre>\n<\/p><\/div>\n
1<\/span> package<\/span> utils; <\/span> 2<\/span> 3<\/span> import<\/span> java.io.FileOutputStream; <\/span> 4<\/span> 5<\/span> import<\/span> javax.xml.parsers.DocumentBuilder; <\/span> 6<\/span> import<\/span> javax.xml.parsers.DocumentBuilderFactory; <\/span> 7<\/span> import<\/span> javax.xml.transform.Transformer; <\/span> 8<\/span> import<\/span> javax.xml.transform.TransformerFactory; <\/span> 9<\/span> import<\/span> javax.xml.transform.dom.DOMSource; <\/span>10<\/span> import<\/span> javax.xml.transform.stream.StreamResult; <\/span>11<\/span> 12<\/span> import<\/span> org.w3c.dom.Document; <\/span>13<\/span> 14<\/span> \/\/<\/span>\u5de5\u5177\u7c7b\u7684\u6240\u6709\u65b9\u6cd5\u90fd\u662f\u9759\u6001\u7684\uff08\u7ea6\u5b9a\u4fd7\u6210\uff09<\/span> 15<\/span> public<\/span> class<\/span> XmlUtils { <\/span>16<\/span> 17<\/span> private<\/span> static<\/span> String filename = \"src\/exam.xml\"; <\/span>18<\/span> 19<\/span> public<\/span> static<\/span> Document getDocument() throws<\/span> Exception{ <\/span>20<\/span> 21<\/span> DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); <\/span>22<\/span> DocumentBuilder builder = factory.newDocumentBuilder(); <\/span>23<\/span> return<\/span> builder.parse(filename); <\/span>24<\/span> 25<\/span> } <\/span>26<\/span> 27<\/span> public<\/span> static<\/span> void<\/span> write2Xml(Document document) throws<\/span> Exception{ <\/span>28<\/span> 29<\/span> TransformerFactory factory = TransformerFactory.newInstance(); <\/span>30<\/span> Transformer tf = factory.newTransformer(); <\/span>31<\/span> tf.transform(new<\/span> DOMSource(document), new<\/span> StreamResult(new<\/span> FileOutputStream(filename))); <\/span>32<\/span> 33<\/span> } <\/span>34<\/span> 35<\/span> }<\/pre>\n<\/p><\/div>\n
1<\/span> package<\/span> dao; <\/span> 2<\/span> 3<\/span> import<\/span> org.w3c.dom.Document; <\/span> 4<\/span> import<\/span> org.w3c.dom.Element; <\/span> 5<\/span> import<\/span> org.w3c.dom.NodeList; <\/span> 6<\/span> 7<\/span> import<\/span> bean.Student; <\/span> 8<\/span> import<\/span> exception.StudentNotExistException; <\/span> 9<\/span> import<\/span> utils.XmlUtils; <\/span>10<\/span> 11<\/span> public<\/span> class<\/span> StudentDao { <\/span>12<\/span> 13<\/span> public<\/span> void<\/span> add(Student s){ <\/span>14<\/span> 15<\/span> try<\/span> { <\/span>16<\/span> Document document = XmlUtils.getDocument(); <\/span>17<\/span> 18<\/span> \/\/<\/span>\u521b\u5efa\u51fa\u5c01\u88c5\u5b66\u751f\u7684\u6807\u7b7e<\/span> 19<\/span> Element student_tag = document.createElement(\"student\"); <\/span>20<\/span> student_tag.setAttribute(\"idcard\", s.getIdcard()); <\/span>21<\/span> student_tag.setAttribute(\"examid\", s.getExamid()); <\/span>22<\/span> 23<\/span> \/\/<\/span>\u521b\u5efa\u7528\u4e8e\u5c01\u88c5\u5b66\u751f\u59d3\u540d\uff0c\u6240\u5728\u5730\u548c\u6210\u7ee9\u7684\u6807\u7b7e<\/span> 24<\/span> Element name = document.createElement(\"name\"); <\/span>25<\/span> Element location = document.createElement(\"location\"); <\/span>26<\/span> Element grade = document.createElement(\"grade\"