Cet article décrit la méthode d'utilisation de JDom pour implémenter les opérations de rédaction de fichiers XML. Partagez-le pour votre référence, comme suit:
package com.yanek.demo.xml.test; import java.io.file; import java.io.filewriter; import org.jdom.attribute; import org.jdom.document; import org. .Input.SaxBuilder; import org.jdom.output.xmloutputter; classe publique jdomwritexml {/ ** * @param args * / public static void main (String [] args) {newsbuilder sb = new saxBuilder (); Élément ("actions"); document du document (actions); ; AddContent (Action1_Forward1); 1_URL_ATBT1 ); "); action1_forward2 .setAttribute (action1_forward1_name_atbt2); action1_forward2.setAttribute (action1_forward1_url_atbt2); élément action2 = new élément (" action "); acte.AddContent (Action2); attribute path_atbt2 = nouvel attribute (" path "," / user " ); .AddContent (Action2_Forward1); 1_URL_ATBT1 ); jsp "); action2_forward2.setAttribute (action2_forward1_name_atbt2); action2_forward2.setAttribute (action2_forward1_url_atbt2); attribut root_atbt1 = new attribut (" m "," 001 "); ac tions.setAttribute (root_atbt1); try {file f1 = new file (" mystrut.xml "); // xmloutputter xo = new xmloutputter (" ", true," gb2312 "); xmloutputter xo = new xmloutputter (); filewriter fw = new filewriter (f1); xo.output (document, fw); fw.close ();} catch (exception e) {e.printStackTrace ();} // System.out.println (document.tostring ());
Générer un fichier XML:
<? xml version = "1.0" Encoding = "utf-8"?> <actions m = "001"> <action path = "/ test"> <usine name = "success" url = "test.jsp" /> <Forward Name = "FAILS" url = "FAILS.JSP" /> </ Action> <Action path = "/ user"> <Forward Name = "Success" url = "test.jsp" /> <Forward Name = " Échec "url =" échec.jsp "/> </ action> </actions>
J'espère que cet article sera utile à la programmation Java de tous.