我获得到一个xml的字符串现在要从中取出相应信息请问有什么方便快捷的方法吗 最好能给出例子
String xmlstring=
<?xml version="1.0" encoding="UTF8"?>
<login_info code="1" tsession="232133213231">
<user_info>
<mobile>1</mobile>
<login_name>abc</login_name>
<nick_name>aaaa</nick_name>
</user_info>
</login_info>
我要取出 "abc" 存入 loginname 和 "aaaa" 存入 nickname
即: loginname="abc" nickname= "aaaa"
希望给出详细步骤 谢谢!
---------------------------------------------------------------
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
try {
db = dbf.newDocumentBuilder();
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
}
Document doc = null;
try {
doc = db.parse(xmlstring);// xml地址
} catch (Exception e) {
e.printStackTrace();
}
Element root = doc.getDocumentElement();
NodeList nl = root.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node aNode = nl.item(i);
if (aNode.getNodeType() == Node.ELEMENT_NODE) {
if (aNode.getNodeName().equals("user_info")) {
NodeList gnl = aNode.getChildNodes();
for (int j = 0; j < gnl.getLength(); j++) {
Node gNode = gnl.item(j);
if (gNode.getNodeType() == Node.ELEMENT_NODE) {
if (gNode
.getNodeName()
.equals("login_name")) {
String userName=getElementText((Element)gNode);//保存userName
}
else if (gNode
.getNodeName()
.equals("nick_name")) {
String nickName=getElementText((Element)gNode);//保存userName
}
}
}
}
}
/// }和个数你自己调整一下,另一个你照着写一下吧
private String getElementText(Element e) {
NodeList nl = e.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node n = nl.item(i);
if (n instanceof org.w3c.dom.Text) {
return n.getNodeValue();
}
}
return "";
}
摘自http://community.csdn.net/Expert/TopicView3.asp?id=4197716
String xmlstring=
<?xml version="1.0" encoding="UTF8"?>
<login_info code="1" tsession="232133213231">
<user_info>
<mobile>1</mobile>
<login_name>abc</login_name>
<nick_name>aaaa</nick_name>
</user_info>
</login_info>
我要取出 "abc" 存入 loginname 和 "aaaa" 存入 nickname
即: loginname="abc" nickname= "aaaa"
希望给出详细步骤 谢谢!
---------------------------------------------------------------
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
try {
db = dbf.newDocumentBuilder();
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
}
Document doc = null;
try {
doc = db.parse(xmlstring);// xml地址
} catch (Exception e) {
e.printStackTrace();
}
Element root = doc.getDocumentElement();
NodeList nl = root.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node aNode = nl.item(i);
if (aNode.getNodeType() == Node.ELEMENT_NODE) {
if (aNode.getNodeName().equals("user_info")) {
NodeList gnl = aNode.getChildNodes();
for (int j = 0; j < gnl.getLength(); j++) {
Node gNode = gnl.item(j);
if (gNode.getNodeType() == Node.ELEMENT_NODE) {
if (gNode
.getNodeName()
.equals("login_name")) {
String userName=getElementText((Element)gNode);//保存userName
}
else if (gNode
.getNodeName()
.equals("nick_name")) {
String nickName=getElementText((Element)gNode);//保存userName
}
}
}
}
}
/// }和个数你自己调整一下,另一个你照着写一下吧
private String getElementText(Element e) {
NodeList nl = e.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node n = nl.item(i);
if (n instanceof org.w3c.dom.Text) {
return n.getNodeValue();
}
}
return "";
}
摘自http://community.csdn.net/Expert/TopicView3.asp?id=4197716