发布网友 发布时间:2022-04-23 10:05
共3个回答
热心网友 时间:2023-10-10 19:26
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<result>\n<resultcode>0</resultcode>\n<taskcode>20130213231023</taskcode>\n<errordescription>发送成功</errordescription>\n</result>";
long start = System.currentTimeMillis();
try {
SAXBuilder builder = new SAXBuilder();
org.jdom.Document doc = builder.build(new ByteArrayInputStream(xml
.getBytes("UTF-8")));
Element element = doc.getRootElement();
List allChildren = element.getChildren();
for (int i = 0; i < allChildren.size(); i++) {
System.out.print(((Element) allChildren.get(i)).getName());
System.out.print(":");
System.out.println(((Element) allChildren.get(i)).getText());
}
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
long end = System.currentTimeMillis();
System.out.println("耗时:" + (end - start) + "ms");
热心网友 时间:2023-10-10 19:27
http://developer.51cto.com/art/200903/117512.htm
有四种方式 你参考下
现在都用JDOM或者DOM4J解析XML了
推荐你用DOM4J
1.导入dom4jJAR包
2.编写好XML文件
3.编写代码
package com.alisoft.facepay.framework.bean;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
/**
*
* @author
* Dom4j 解析XML文档
*/
public class Dom4jDemo implements XmlDocument {
//定义你xml文件存放的位置
String fileName="D://parse.xml"
public void parserXml(String fileName) {
File XmlName=new File(fileName);
SAXReader saxReader = new SAXReader();
try {
//读取XML文件为DOCUMENT对象
Document document = saxReader.read(XmlName);
//获取XML文件的根节点,也就是你xml文件的result
Element result=document.getRootElement();
//循环取出result的子节点resultcode、taskcode、errordescription 并输出个个节点的名称和值
for(Iterator i = result.elementIterator(); i.hasNext();){
Element node = (Element) i.next();
System.out.println(node.getName()+":"+node.getText());
}
} catch (DocumentException e) {
System.out.println(e.getMessage());
}
System.out.println("dom4j parserXml");
}
}
打印出的数据:
resultcode:0
taskcode:20130213231023
errordescription:发送成功
dom4j parserXml追问麻烦发个dom4jJAR包
百度了二十多个网页都是木有!
追答
热心网友 时间:2023-10-10 19:27
你有接口吗?你有短信猫吗?发短信不要钱嘛?别想不劳而获,自己在网上搜下一大堆发短信的!!