<?xml version="1.0" encoding="UTF-8"?>
<buildCloud name="buildCloud"> <job id="1">test</job> <result id="">success</result> <downTasks name="downTasks" startTime='10' endTime='20'>3 <downTask name="ICPCI" startTime='10' endTime='20'>3</downTask> <downTask name="BuildCloud" startTime='20' endTime='30'>3</downTask> </downTasks> <agentTask name="CCT" startTime='1' endTime='2'> 3 <task name="cct_common" startTime='11' endTime='21'>3</task> <task name="cct_anget" startTime='111' endTime='121'> 3 </task> </agentTask>
<compiste name="simint_commonLP" startTime='A' endTime='B'>
3
<current name="simintCurr" startTime='VV' endTime='LL'>
3
<agentTask name="CCTS" startTime='mk' endTime='LM'>
3
<task name="cct_commonS" startTime='1' endTime='ds'>3</task>
<task name="cct_angetS" startTime='1' endTime='sd2'>3</task>
</agentTask>
</current>
</compiste>
<current name="simint_common" startTime='A' endTime='V'>
3
<compiste name="simint_commonLP" startTime='A' endTime='B'>
3
<current name="simintCurr" startTime='VV' endTime='LL'>
3
<agentTask name="CCTS" startTime='mk' endTime='LM'>
3
<task name="cct_commonS" startTime='1' endTime='ds'>3</task>
<task name="cct_angetS" startTime='1' endTime='sd2'>3</task>
</agentTask>
</current>
</compiste>
</current>
</buildCloud>
package task;
import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List;
import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.JDOMException; import org.jdom2.input.SAXBuilder;
public class Parse2 { public static Element getRootElement(String file) { try { Document doc = new SAXBuilder().build(new File(file)); return doc.getRootElement(); } catch (JDOMException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
public static List<String> getElementStringList(String nodeName,
Element root) {
List<Element> childList = root.getChildren();
List<String> list = new ArrayList<String>();
for (Element e : childList) {
if (e.getAttribute("name") != null) {
String content = e.getName() + ","
+ e.getAttributeValue("name") + ","
+ e.getAttributeValue("startTime") + ","
+ e.getAttributeValue("endTime") + ","
+ e.getText().trim();
list.add(content);
list.addAll(getElementStringList(e.getName(), e));
}
}
return list;
}
public static String getElementList(String nodeName, Element root) {
List<Element> childList = root.getChildren();
StringBuffer buffer = new StringBuffer();
for (Element e : childList) {
if (e.getAttribute("name") != null) {
String content = e.getName() + ","
+ e.getAttributeValue("name") + ","
+ e.getAttributeValue("startTime") + ","
+ e.getAttributeValue("endTime") + ","
+ e.getText().trim();
buffer.append(content).append("\n");
// System.out.println(content);
buffer.append(getElementList(e.getName(), e));
}
}
return buffer.toString();
}
public static void main(String args[]) {
Element root = getRootElement("D:\\parse.xml");
// String s = getElementList("buildCloud", root);
// System.err.println(s);
List<String> list = getElementStringList("buildCloud", root);
for (String str : list) {
System.out.println(str);
}
}
}