Este artigo descreve o método de usar o JDOM para implementar operações de gravação de arquivos XML. Compartilhe -o para sua referência, como segue:
pacote com.yanek.demo.xml.test; importar java.io.file; importar java.io.filewriter; importar org.jdom.attribute; importar org.jdom.document; importar org .input.saxbuilder; importar org.jdom.output.xmloutputter; public class JdomWritexml { / ** * @param args * / public static void main (string [] args) {saxbuilder sb = new saxbuilder (); Elemento "Ações"); Atributo Class_atbt1 = Novo atributo ("Classe", "com.mystuts.demo.loginaction"); AddContent (Action1_forward1); 1_URL_ATBT1 ); "); action1_forward2 .SetAttribute (action1_forward1_name_atbt2); ação1_forward2.SetAttribute (Action1_forward1_url_atbt2); elemento action2 = novo elemento (" ação ");" ACTIONS.ADDCONTENT (Action2); atributo PATH_ATBT2 = New "Path" "" ); .AddContent (Action2_forward1); 1_URL_ATBT1 ); JSP "); ação2_forward2.SetAttribute (action2_forward1_name_atbt2); ação2_forward2.SetAttribute (action2_forward1_url_atbt2); attribute root_atbt1 = new attribute (" m "," 001 "); actions.setattribute (_Atbt1); myStruts.xml "); // xmloutputter XO = new XmlOutputter (" ", true," gb2312 "); xmloutputter xo = new xmloutputter (); fileWriter fw = new FileWriter (f1); xo.output (document,); fw.close ();
Gere um arquivo XML:
<? xml versão = "1.0" coding = "utf-8"?> <Ações m = "001"> <ação path = " /test"> <forward name = "success" url = "test.jsp" /> <Nome para a Forward = "Faulth" URL = "FAIUNT.JSP" /> < /ACTION> <Action Path = " /User"> <Nome para a Forward = "Sucesso" URL = "Test.jsp" /> <Nome para a Forward = " falha "url =" falha.jsp "/> </ction> </ações>
Espero que este artigo seja útil para a programação Java de todos.