那位高手 给我把这段 XML 代码 用 dom4j 解吸出来呀 <?xml version="1.0" encoding="UTF-8"?>
<im apicode ="0003">
<sms>
<deliver mobile="13691324865" sm_id="160" code="0" desc="发送成功。" />
</sms>
</im>我想得到 apicode 、 mobile 、 sm_id 、 code 、 desc的值
<im apicode ="0003">
<sms>
<deliver mobile="13691324865" sm_id="160" code="0" desc="发送成功。" />
</sms>
</im>我想得到 apicode 、 mobile 、 sm_id 、 code 、 desc的值
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;public class Domtest {
public static void main(String[] args){
new Domtest();
}
public Domtest(){
DocumentBuilderFactory domfac=DocumentBuilderFactory.newInstance();
try {
DocumentBuilder dombuilder=domfac.newDocumentBuilder();
InputStream is;
is = new FileInputStream("domtest.xml");
Document doc=dombuilder.parse(is);
Element root=doc.getDocumentElement();
String apicode = root.getAttribute("apicode");
System.out.println("apicode:"+apicode);
NodeList smss = root.getChildNodes();
if(smss != null){
for(int i=0;i<smss.getLength();i++){
Node sms = smss.item(i);
for(Node node = sms.getFirstChild();node != null;node = node.getNextSibling()){
if(node.getNodeType() == Node.ELEMENT_NODE){
if(node.getNodeName().equals("deliver")){
String mobile = node.getAttributes().getNamedItem("mobile").getNodeValue();
System.out.println("mobile:"+mobile);
String sm_id = node.getAttributes().getNamedItem("sm_id").getNodeValue();
System.out.println("sm_id:"+sm_id);
String code = node.getAttributes().getNamedItem("code").getNodeValue();
System.out.println("code:"+code);
String desc = node.getAttributes().getNamedItem("desc").getNodeValue();
System.out.println("desc:"+desc);
}
}
}
}
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} }
}
import java.net.MalformedURLException;
import java.util.*;
import org.dom4j.*;
import org.dom4j.io.*; public class Domtest { public static void main(String arge[]) {
File f = new File("domtest.xml");
SAXReader reader = new SAXReader();
Document doc;
try {
doc = reader.read(f);
Element root = doc.getRootElement();
Element sms;
System.out.println("apicode : " + root.attributeValue("apicode"));
for (Iterator i = root.elementIterator("sms"); i.hasNext();) {
sms = (Element) i.next();
Element deliver = sms.element("deliver");
System.out.println("mobile " + deliver.attributeValue("mobile"));
System.out.println("sm_id " + deliver.attributeValue("sm_id"));
System.out.println("code " + deliver.attributeValue("code"));
System.out.println("desc " + deliver.attributeValue("desc"));
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
}
}