大家好,又见面了,我是你们的朋友全栈君。
本文转自点击打开链接
import java.io.StringReader;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.xml.sax.InputSource;
public class Test {
public static void main(String[] args) throws Exception {
String Testxml="<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<CSubmitState xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://tempuri.org/\">\n" + " <State>054</State>\n" + " <MgID>170479913</MgID>\n" + " <Msgtate>adadafa</Msgtate>\n" + " <Rserve>01</Rserve>\n" + "</CSubmitState>";
org.xml.sax.InputSource in = new org.xml.sax.InputSource(new StringReader(Testxml));
in.setEncoding("UTF-8");
SAXReader reader = new SAXReader();
Document document = reader.read(in);
Element root = document.getRootElement();
List<Element> elements = root.elements();
for(Iterator<Element> it = elements.iterator();it.hasNext();){
Element element = it.next();
System.out.println(element.getName() + " : " + element.getTextTrim());
if("Reserve".equals(element.getName())) {
System.out.println(element.getName() + " : " + element.getTextTrim());
}
}
}
}
————————————————————————————————————————————–
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/136070.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...