java 分析 xml 字符串的问题
我现在有一个字符串,其内容是XML格式的,样式大致如下:<?xmlversion="1.0"encoding="utf-8"?><stringxmlns="http://...
我现在有一个字符串,其内容是XML格式的,样式大致如下:
<?xml version="1.0" encoding="utf-8"?><string xmlns="http://www.webserviceX.NET"><NewDataSet> <Table> <Country>China</Country> <City>Beijing</City> </Table> <Table> <Country>China</Country> <City>Hohhot</City> </Table> <Table> <Country>China</Country> <City>Tianjin / Zhangguizhu</City> </Table> <Table> <Country>China</Country> <City>Taiyuan</City> </Table> <Table> <Country>China</Country> <City>Changsha</City> </Table> <Table> <Country>China</Country> <City>Guangzhou</City> </Table> <Table> <Country>China</Country> <City>Haikou</City> </Table> <Table> <Country>China</Country> <City>Guilin</City> </Table> <Table> <Country>China</Country> <City>Nanning</City> </Table> <Table> <Country>China</Country> <City>Shantou</City> </Table> <Table> <Country>China</Country> <City>Shenzhen</City> </Table> <Table> <Country>China</Country> <City>Zhanjiang</City> </Table> <Table> <Country>China</Country> <City>Zhengzhou</City> </Table>
我用如下的方法试图把City节点的内容取出,但一点反映都没有,请帮我看看问题出在哪?
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dbf.newDocumentBuilder();
Document doc = builder.parse(new InputSource(new ByteArrayInputStream(xmlString.getBytes())));
NodeList nl = doc.getElementsByTagName("City");
for (int count = 0; count < nl.getLength(); count++) {
Node n = nl.item(count);
System.out.println(n);
} 展开
<?xml version="1.0" encoding="utf-8"?><string xmlns="http://www.webserviceX.NET"><NewDataSet> <Table> <Country>China</Country> <City>Beijing</City> </Table> <Table> <Country>China</Country> <City>Hohhot</City> </Table> <Table> <Country>China</Country> <City>Tianjin / Zhangguizhu</City> </Table> <Table> <Country>China</Country> <City>Taiyuan</City> </Table> <Table> <Country>China</Country> <City>Changsha</City> </Table> <Table> <Country>China</Country> <City>Guangzhou</City> </Table> <Table> <Country>China</Country> <City>Haikou</City> </Table> <Table> <Country>China</Country> <City>Guilin</City> </Table> <Table> <Country>China</Country> <City>Nanning</City> </Table> <Table> <Country>China</Country> <City>Shantou</City> </Table> <Table> <Country>China</Country> <City>Shenzhen</City> </Table> <Table> <Country>China</Country> <City>Zhanjiang</City> </Table> <Table> <Country>China</Country> <City>Zhengzhou</City> </Table>
我用如下的方法试图把City节点的内容取出,但一点反映都没有,请帮我看看问题出在哪?
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dbf.newDocumentBuilder();
Document doc = builder.parse(new InputSource(new ByteArrayInputStream(xmlString.getBytes())));
NodeList nl = doc.getElementsByTagName("City");
for (int count = 0; count < nl.getLength(); count++) {
Node n = nl.item(count);
System.out.println(n);
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询