JAVA代码解析方法返回的XML数据,分别取出item标签中的值
<data><message><status>6</status><value>识别成功</value></message><cardsinfo><cardtype="6...
<data>
<message>
<status>6</status>
<value>识别成功</value>
</message>
<cardsinfo>
<card type="6">
<item desc="保留"><![CDATA[轻轻巧巧]]></item>
<item desc="号牌号码"><![CDATA[*888084]]></item>
<item desc="车辆类型"><![CDATA[小型轿车]]></item>
<item desc="所有人"><![CDATA[丹丹]]></item>
<item desc="住址"><![CDATA[囊簿囊鬻鞲鑫鞲濉鞲篝蘩彝耩濉攮糍瓣麟]]></item>
<item desc="品牌型号"><![CDATA[飞燕883086W21613K5]]></item>
<item desc="车辆识别代号"><![CDATA[5594888]]></item>
<item desc="发动机号码"><![CDATA[416098]]></item>
<item desc="注册日期"><![CDATA[2012-11-22]]></item>
<item desc="发证日期"><![CDATA[]]></item>
<item desc="使用性质"><![CDATA[非营运]]></item>
</card>
</cardsinfo>
</data> 展开
<message>
<status>6</status>
<value>识别成功</value>
</message>
<cardsinfo>
<card type="6">
<item desc="保留"><![CDATA[轻轻巧巧]]></item>
<item desc="号牌号码"><![CDATA[*888084]]></item>
<item desc="车辆类型"><![CDATA[小型轿车]]></item>
<item desc="所有人"><![CDATA[丹丹]]></item>
<item desc="住址"><![CDATA[囊簿囊鬻鞲鑫鞲濉鞲篝蘩彝耩濉攮糍瓣麟]]></item>
<item desc="品牌型号"><![CDATA[飞燕883086W21613K5]]></item>
<item desc="车辆识别代号"><![CDATA[5594888]]></item>
<item desc="发动机号码"><![CDATA[416098]]></item>
<item desc="注册日期"><![CDATA[2012-11-22]]></item>
<item desc="发证日期"><![CDATA[]]></item>
<item desc="使用性质"><![CDATA[非营运]]></item>
</card>
</cardsinfo>
</data> 展开
2个回答
2016-07-01
展开全部
package com;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
public class TestXML {
public static void main(String[] args) {
StringBuffer sb=new StringBuffer("<data>");
sb.append("<message>");
sb.append("<status>6</status>");
sb.append("<value>识别成功</value>");
sb.append("</message>");
sb.append("<cardsinfo>");
sb.append("<card type='6'>");
sb.append("<item desc='保留'><![CDATA[轻轻巧巧]]></item>");
sb.append("<item desc='号牌号码'><![CDATA[*888084]]></item>");
sb.append("</card>");
sb.append("</cardsinfo>");
sb.append("</data>");
try {
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder
.parse(new InputSource((new StringReader(sb.toString()))));
NodeList list = doc.getElementsByTagName("item");
System.out.println("------------节点item如下:------------");
for (int i = 0; i < list.getLength(); i++) {
Node book = list.item(i);
System.out.println("\t节点=" + i + "\t内容="
+ book.getFirstChild().getNodeValue());
}
System.out.println("------------结束------------");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
public class TestXML {
public static void main(String[] args) {
StringBuffer sb=new StringBuffer("<data>");
sb.append("<message>");
sb.append("<status>6</status>");
sb.append("<value>识别成功</value>");
sb.append("</message>");
sb.append("<cardsinfo>");
sb.append("<card type='6'>");
sb.append("<item desc='保留'><![CDATA[轻轻巧巧]]></item>");
sb.append("<item desc='号牌号码'><![CDATA[*888084]]></item>");
sb.append("</card>");
sb.append("</cardsinfo>");
sb.append("</data>");
try {
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder
.parse(new InputSource((new StringReader(sb.toString()))));
NodeList list = doc.getElementsByTagName("item");
System.out.println("------------节点item如下:------------");
for (int i = 0; i < list.getLength(); i++) {
Node book = list.item(i);
System.out.println("\t节点=" + i + "\t内容="
+ book.getFirstChild().getNodeValue());
}
System.out.println("------------结束------------");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐于2018-03-26
展开全部
实现思路:可以通过w3c的dom解析器进行操作。
举例:
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class DomTest1
{
public static void main(String[] args) throws Exception
{
// step 1: 获得dom解析器工厂(工作的作用是用于创建具体的解析器)
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// System.out.println("class name: " + dbf.getClass().getName());
// step 2:获得具体的dom解析器
DocumentBuilder db = dbf.newDocumentBuilder();
// System.out.println("class name: " + db.getClass().getName());
// step3: 解析一个xml文档,获得Document对象(根结点)
Document document = db.parse(new File("candidate.xml"));
NodeList list = document.getElementsByTagName("PERSON");
for(int i = 0; i < list.getLength(); i++)
{
Element element = (Element)list.item(i);
String content = element.getElementsByTagName("NAME").item(0).getFirstChild().getNodeValue();
System.out.println("name:" + content);
content = element.getElementsByTagName("ADDRESS").item(0).getFirstChild().getNodeValue();
System.out.println("address:" + content);
content = element.getElementsByTagName("TEL").item(0).getFirstChild().getNodeValue();
System.out.println("tel:" + content);
content = element.getElementsByTagName("FAX").item(0).getFirstChild().getNodeValue();
System.out.println("fax:" + content);
content = element.getElementsByTagName("EMAIL").item(0).getFirstChild().getNodeValue();
System.out.println("email:" + content);
System.out.println("--------------------------------------");
}
}
}
举例:
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class DomTest1
{
public static void main(String[] args) throws Exception
{
// step 1: 获得dom解析器工厂(工作的作用是用于创建具体的解析器)
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// System.out.println("class name: " + dbf.getClass().getName());
// step 2:获得具体的dom解析器
DocumentBuilder db = dbf.newDocumentBuilder();
// System.out.println("class name: " + db.getClass().getName());
// step3: 解析一个xml文档,获得Document对象(根结点)
Document document = db.parse(new File("candidate.xml"));
NodeList list = document.getElementsByTagName("PERSON");
for(int i = 0; i < list.getLength(); i++)
{
Element element = (Element)list.item(i);
String content = element.getElementsByTagName("NAME").item(0).getFirstChild().getNodeValue();
System.out.println("name:" + content);
content = element.getElementsByTagName("ADDRESS").item(0).getFirstChild().getNodeValue();
System.out.println("address:" + content);
content = element.getElementsByTagName("TEL").item(0).getFirstChild().getNodeValue();
System.out.println("tel:" + content);
content = element.getElementsByTagName("FAX").item(0).getFirstChild().getNodeValue();
System.out.println("fax:" + content);
content = element.getElementsByTagName("EMAIL").item(0).getFirstChild().getNodeValue();
System.out.println("email:" + content);
System.out.println("--------------------------------------");
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询