XML 解析

zhangchao0323 2007-07-10 10:13:34
那位高手 给我把这段 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

的值

...全文
226 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangchao0323 2007-07-12
  • 打赏
  • 举报
回复
恩 谢谢~~加我 MSN : zhangchao0323@hotmail.com
haidifotiaoqiang 2007-07-10
  • 打赏
  • 举报
回复
这是dom4j的,希望尽量自己写写,写过了就是自己的了

import java.io.*;
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();
}

}
}
haidifotiaoqiang 2007-07-10
  • 打赏
  • 举报
回复
我运行没问题
haidifotiaoqiang 2007-07-10
  • 打赏
  • 举报
回复
package test;
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();
}

}

}

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧