Establish, modify the XML document with DOM4J, and resolve formatting outputs and Chinese issues

xiaoxiao2021-03-06  109

Establish, modify the XML document with DOM4J, and resolve formatting outputs and Chinese issues

DOM4J TUTORIALS </ TITLE> </ book> <book show = "no" > <Title> Lucene Studing </ Title> </ book> <book show = "no"> <title> Lucene in action </ title> </ book> <owner> O'Reilly </ Owner> </ books></p> <p>---------------------</p> <p>Package com.holen.dom4j;</p> <p>Import java.io.file;</p> <p>Import java.io.filewriter;</p> <p>Import java.util.iterator;</p> <p>Import java.util.list;</p> <p>Import org.dom4j.attribute;</p> <p>Import org.dom4j.document;</p> <p>Import org.dom4j.documenthelper;</p> <p>Import org.dom4j.element;</p> <p>Import Org.dom4j.io.outputFormat;</p> <p>Import org.dom4j.io.saxreader;</p> <p>Import org.dom4j.io.xmlwriter;</p> <p>/ **</p> <p>* @Author holin chen</p> <p>* /</p> <p>Public class dom4jdemo {</p> <p>Public Dom4jdemo () {</p> <p>}</p> <p>/ **</p> <p>* Establish an XML document, the document name is determined by the input parameter</p> <p>* @Param FileName needs to be established file name</p> <p>* @return returns the result of the operation, 0 tables fail, 1 table is successful</p> <p>* /</p> <p>Public int createXmlfile (String filename) {</p> <p>/ ** Return to the results, 0 tables fail, 1 table is successful * /</p> <p>Int returnvalue = 0;</p> <p>/ ** Establish Document object * /</p> <p>Document Document = documenthelper.createdocument ();</p> <p>/ ** Establish the root of the XML document BOOKS * /</p> <p>ELEMENT BOOKSELEMENT = Document.AddeElement ("Books");</p> <p>/ ** Add a line of comments * /</p> <p>BookSelement.addcomment ("this is a test for Dom4j, Holen, 2004.9.11");</p> <p>/ ** Join the first BOOK node * /</p> <p>Element Bookelement = Bookselement.addelement ("book"); "BOOK");</p> <p>/ ** Add to show parameter content * /</p> <p>Bookelement.Addattribute ("Show", "Yes");</p> <p>/ ** Add Title Node * /</p> <p>Element TitleElement = Bookelement.addeElement ("Title");</p> <p>/ ** Set content for Title * /</p> <p>TitleElement.Settext ("Dom4j Tutorials");</p> <p>/ ** Similar to the two BOOK * /</p> <p>Bookelement = bookselement.addelement ("book"); Bookelement.addattribute ("Show", "YES");</p> <p>TitleElement = Bookelement.addeElement ("Title");</p> <p>TitleElement.Settext ("Lucene Studing");</p> <p>Bookelement = BookSelement.addelement ("book");</p> <p>Bookelement.addattribute ("Show", "NO");</p> <p>TitleElement = Bookelement.addeElement ("Title");</p> <p>TitleElement.Settext ("Lucene In Action");</p> <p>/ ** Add to Owner Node * /</p> <p>Element OwneRelement = BookSelement.addeElent ("Owner");</p> <p>OwneRelement.Settext ("O'Reilly");</p> <p>Try {</p> <p>/ ** Write the contents in Document * /</p> <p>XMLWRITER Writer = New FILEWRITER (New File (FileName)))</p> <p>Writer.write (Document);</p> <p>Writer.close ();</p> <p>/ ** Execute success, need to return 1 * /</p> <p>ReturnValue = 1;</p> <p>} catch (exception ex) {</p> <p>EX.PrintStackTrace ();</p> <p>}</p> <p>Return ReturnValue;</p> <p>}</p> <p>/ **</p> <p>* Modify the content in the XML file and save as a new file</p> <p>* Keep how to add nodes in DOM4J, modify nodes, delete nodes</p> <p>* @Param FileName Modify Object File</p> <p>* @Param newFileName After modifying saving as this file</p> <p>* @return returns the result of the operation, 0 tables fail, 1 table is successful</p> <p>* /</p> <p>Public int modixmlfile (string newfilename) {</p> <p>Int returnvalue = 0;</p> <p>Try {</p> <p>SAXReader saxreader = new saxreader ();</p> <p>Document Document = SAXReader.read (New File (FileName));</p> <p>/ ** One of the modifications: If the content of the Show parameter in the Book node is YES, modify it into no * /</p> <p>/ ** Find objects with XPath * /</p> <p>List list = document.selectnodes ("/ books / book / @ Show");</p> <p>Iterator it = list.iterator ();</p> <p>While (ore.hasnext ()) {</p> <p>Attribute attribute = (attribute) iter.next ();</p> <p>IF (attribute.getvalue (). Equals ("yes")) {</p> <p>Attribute.SetValue ("no");</p> <p>}</p> <p>}</p> <p>/ **</p> <p>* Two two modifications: change the Owner item content to tSHINGHUA</p> <p>* Add the Date node in the Owner node, the Date node is 2004-09-11, add a parameter Type for the Date node</p> <p>* /</p> <p>List = Document.selectNodes ("/ books / Owner");</p> <p>iter = list.iterator ();</p> <p>IF (iter.hasnext ()) {ELEMENT OWNERELEMENT = (Element) iter.next ();</p> <p>OwneRelement.setText ("TSHINGHUA");</p> <p>Element DateElement = OwneRelement.addeElement ("Date");</p> <p>DateElement.Settext ("2004-09-11");</p> <p>DateElement.addattribute ("Type", "Gregorian Calendar";</p> <p>}</p> <p>/ ** Three modifications: If the title content is DOM4J Tutorials, delete the node * /</p> <p>List = document.selectnodes ("/ books / book");</p> <p>iter = list.iterator ();</p> <p>While (ore.hasnext ()) {</p> <p>ELEMENT BOOKELEMENT = (Element) iTer.next ();</p> <p>Iterator itrator = Bookelement.elementIterator ("Title");</p> <p>While (item.hasnext ()) {</p> <p>Element TitleElement = (Element) Iterator.next ();</p> <p>IF (TitleElement.getText (). Equals ("DOM4J Tutorials")) {</p> <p>Bookelement.remove (TitleElement);</p> <p>}</p> <p>}</p> <p>}</p> <p>Try {</p> <p>/ ** Write the contents in Document * /</p> <p>XMLWRITER Writer = New Filewriter (New File (New File (New File (NewFileName));</p> <p>Writer.write (Document);</p> <p>Writer.close ();</p> <p>/ ** Execute success, need to return 1 * /</p> <p>ReturnValue = 1;</p> <p>} catch (exception ex) {</p> <p>EX.PrintStackTrace ();</p> <p>}</p> <p>} catch (exception ex) {</p> <p>EX.PrintStackTrace ();</p> <p>}</p> <p>Return ReturnValue;</p> <p>}</p> <p>/ **</p> <p>* Format XML document and solve Chinese problems</p> <p>* @Param FileName</p> <p>* @Return</p> <p>* /</p> <p>Public int formatXMLFile (String filename) {</p> <p>Int returnvalue = 0;</p> <p>Try {</p> <p>SAXReader saxreader = new saxreader ();</p> <p>Document Document = SAXReader.read (New File (FileName));</p> <p>XMLWRITER OUTPUT = NULL;</p> <p>/ ** Format output, type IE browsing * /</p> <p>Outputformat format = outputformat.createprettyPrint ();</p> <p>/ ** Specify XML character set encoding * /</p> <p>Format.setencoding ("GBK");</p> <p>Output = New XMLWRITER (New File (New File (Filename), Format);</p> <p>Output.write (document);</p> <p>Output.close ();</p> <p>/ ** Execute success, need to return 1 * /</p> <p>ReturnValue = 1;</p> <p>} catch (exception ex) {</p> <p>EX.PrintStackTrace ();</p> <p>}</p> <p>Return ReturnValue;</p> <p>}</p> <p>Public static void main (string [] args) {DOM4JDEMO TEMP = New Dom4jdemo ();</p> <p>System.out.println (Temp.createxmlfile))))); "D: //holen.xml");</p> <p>System.out.println (Temp.Modixmlfile ("D: //holen.xml", "D: //holen2.xml");</p> <p>System.out.println (Temp.FormatXMLFile ("D: //holen2.xml));</p> <p>}</p> <p>}</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-104423.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="104423" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.056</b>, SQL: <b>12</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'rzJ_2Bcr9_2Bq6ISiL8cdkV4vNKJN6qZD7lM_2BAtjgnroIpTyeQurHMGLpbSbfOIx1Prw9AL_2Fe4rU80OnDeaHwBD6PA_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>