发表评论
一、 例1
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.xml.sax.InputSource;
public class Test {
public static void main(String[] args) throws Exception {
//拼接xml格式其中notify节点是不固定数量的
String xml="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
+ "<notifyBusiness>"
+ "<notify>"
+ "<busiId>373394</busiId>"
+ "<busiNo>000324232214319</busiNo>"
+ "<busiTime>2013-04-10 09:52:20</busiTime>"
+ "<notifyId>3745</notifyId>"
+ "<para1></para1>"
+ "<para2>319</para2>"
+ "<para3></para3>"
+ "<para4></para4>"
+ "<para5>1110000</para5>"
+ "<notifyType>10</notifyType>"
+ "</notify>"
+ "<notify>"
+ "<busiId>12345</busiId>"
+ "<busiNo>482892842822</busiNo>"
+ "<busiTime>2013-04-10 09:52:20</busiTime>"
+ "<notifyId>3745</notifyId>"
+ "<para1></para1>"
+ "<para2>319</para2>"
+ "<para3></para3>"
+ "<para4></para4>"
+ "<para5>1110000</para5>"
+ "<notifyType>10</notifyType>"
+ "</notify>"
+ "</notifyBusiness>";
System.out.println(xml);
StringReader read = new StringReader(xml);
InputSource source = new InputSource(read);
SAXBuilder sb = new SAXBuilder();
Document doc = sb.build(source);
Element root = doc.getRootElement();
List jiedian = root.getChildren("notify");
Element xxx = null;
for (int i = 0; i < jiedian.size(); i++)
{
xxx = (Element) jiedian.get(i);
String busiId = xxx.getChild("busiId").getText();
String busiNo = xxx.getChild("busiId").getText();
String busiTime = xxx.getChild("busiTime").getText();
String notifyId = xxx.getChild("notifyId").getText();
String para1 = xxx.getChild("para1").getText();
String para2 = xxx.getChild("para2").getText();
String para3 = xxx.getChild("para3").getText();
String para4 = xxx.getChild("para4").getText();
String para5 = xxx.getChild("para5").getText();
String notifyType = xxx.getChild("notifyType").getText();
System.out.println(busiId+busiNo+busiTime+notifyId);
}
}
}
二、 例2
import java.io.IOException;
import java.io.StringReader;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.xml.sax.InputSource;
public class Test2 {
public static void main(String[] args) throws Exception {
//拼接xml字符串
String xml =
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
+"<TXLife>"
+"<TXLifeResponse>"
+"<transExeDate>2014-05-14</transExeDate>"
+"<transExeTime>15:28:22</transExeTime>"
+"<transType>104</transType>"
+"<transMode>14</transMode>"
+"<transRefGUID>b5b7f4a6-0971-4b86-9ca7-ed30fc09dfe6</transRefGUID>"
+"<transResult>"
+"<resultCode>2</resultCode>"
+"<resultInfo>错误描述</resultInfo>"
+"</transResult>"
+"</TXLifeResponse>"
+"</TXLife>";
StringReader read = new StringReader(xml);
InputSource source = new InputSource(read);
SAXBuilder sb = new SAXBuilder();
Document doc = sb.build(source);
Element root = doc.getRootElement();
Element xxx = root.getChild("TXLifeResponse");
String transExeDate = xxx.getChild("transExeDate").getText();
String transExeTime = xxx.getChild("transExeTime").getText();
String transType = xxx.getChild("transType").getText();
String transMode = xxx.getChild("transMode").getText();
String transRefGUID = xxx.getChild("transRefGUID").getText();
Element transResult = xxx.getChild("transResult");
String resultCode = transResult.getChild("resultCode").getText();
String resultInfo = transResult.getChild("resultInfo").getText();
System.out.println(transExeDate+transExeTime+resultCode+resultInfo);
}
}
两个非常简单的使用jdom解析xml的方法,差不多可以适用各种xml情况了~