jdom読み書きXMLファイル
5409 ワード
昨日書いたjdomを使用してxmlファイルを読み書きする例です.
/** XMl*/
public boolean writeXML(String guid, String path) {
boolean temp = false;
List<SynchronousJournal> listSuccess = synchronousJournalManager
.quereySynchronousJournal(guid);
List<SynchronousJournal> listError = synchronousJournalManager
.quereySynchronousJournalErroe(guid);
File file = new File(path);// xml
try {
Element root = new Element("RESULTLIST");//
for (SynchronousJournal synchronousJournal : listSuccess) {
Element result = new Element("RESULT");//
root.addContent(result);//
Element type = new Element("TYPE");//
result.addContent(type);//
Element businessType = new Element("BUSINESSTYPE");//
businessType.addContent(businessType);//
type.setText(synchronousJournal.getBusinesstype());//
/** */
Element success = new Element("SUCCESS");//
success.setText(synchronousJournal.getCount().toString());//
type.addContent(success);//
int a = 0;
for (SynchronousJournal synchronousJournal2 : listError) {
if (synchronousJournal.getBusinesstype().equals(
synchronousJournal2.getBusinesstype()))
a++;
}
Element error = new Element("ERROR");
type.addContent(error);
Element errorcount = new Element("ERRORCOUNT");
error.setText(String.valueOf(a));//
error.addContent(errorcount);
for (SynchronousJournal synchronousJournal2 : listError) {
/** */
if (synchronousJournal.getBusinesstype().equals(
synchronousJournal2.getBusinesstype())) {
Element errorguid = new Element("ERRORGUID");
error.setText(synchronousJournal2.getInfoGuid());//
error.addContent(errorguid);
}
}
}
Document doc = new Document();//
doc.addContent(root);//
Format format = Format.getCompactFormat();
format.setIndent(" ");
XMLOutputter out = new XMLOutputter(format);//
FileWriter fw = new FileWriter(file);//
out.output(doc, fw);// xml
fw.close();//
temp = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
}
return temp;
}
/** xml*/
public static boolean readXml(String path) {
boolean temp = false;
SAXBuilder sb = new SAXBuilder();// JDOM
try {
Document doc = sb.build(path);// xml
Element root = doc.getRootElement();// xml
List list = root.getChildren();// xml
for (int i = 0; i < list.size(); i++) {
Element result = (Element) list.get(i);//
List typelist = result.getChildren("TYPE");
for (int j = 0; j < typelist.size(); j++) {
Element type = (Element) typelist.get(j);// xml
System.out.println(type.getChildText("BUSINESSTYPE"));
String success=type.getChildText("SUCCESS");
System.out.println(" "+success+" ");
Element error = type.getChild("ERROR");// xml
String errorCount = error.getChildText("ERRORCOUNT");//
System.out.println(" "+errorCount+" ");
List errorguid = error.getChildren("ERRORGUID");// xml
String empno1 = " guid:";
for (int a = 0; a < errorguid.size(); a++) {
Element emp1 = (Element) errorguid.get(a);//
empno1 = empno1 + " " + emp1.getText();
}
System.out.println(empno1);
}
// }
}
temp = true;
} catch (JDOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
temp = false;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
temp = false;
}
return temp;
}<?xml version="1.0" encoding="UTF-8"?>
<RESULTLIST>
<RESULT>
<TYPE>
<BUSINESSTYPE> </BUSINESSTYPE>
<SUCCESS>50</SUCCESS>
<ERROR>
<ERRORCOUNT>20</ERRORCOUNT>
<ERRORGUID>1234156456462</ERRORGUID>
<ERRORGUID>1234156456464</ERRORGUID>
</ERROR>
</TYPE>
<TYPE>
<BUSINESSTYPE> </BUSINESSTYPE>
<SUCCESS>50</SUCCESS>
<ERROR>
<ERRORCOUNT>20</ERRORCOUNT>
<ERRORGUID>1234156456463</ERRORGUID>
<ERRORGUID>1234156456464</ERRORGUID>
</ERROR>
</TYPE>
</RESULT>
</RESULTLIST>