xml标签内容获得迷惑

来源:互联网 发布:android算法面试题 编辑:程序博客网 时间:2024/06/07 00:24
package com.suning.ebuy.vgs.airticket.globalImpl;

import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import com.xiao.demo.utils.XmlUtils;

public class XmlTest {


public static void main(String[] args) throws ParserConfigurationException,
SAXException, IOException, Exception {
// TODO Auto-generated method stub
String xml = "<Prices><Price><PsgType>ADT</PsgType></Price><Price><PsgType>CHD</PsgType></Price></Prices>";
Document doc = XmlUtils.getW3CDom(xml);
NodeList nodeList = XmlUtils.runXpath(doc, "//Prices/Price", null);
for (int i = 0; i < nodeList.getLength(); i++) {

Node node = nodeList.item(i);
getPrices(node);
}

}


public static void getPrices(Node node,int i) {
String pricesXml = XmlUtils.nodeAsString(node);
System.err.println(pricesXml);
String psgType = XmlUtils.getValueByXpath(node, "//Price/PsgType");
System.err.println("乘客类型:" + psgType);

}


}

上面的类运行的结果你会猜到么?


<?xml version="1.0" encoding="UTF-8"?><Price><PsgType>ADT</PsgType></Price>
乘客类型:ADT
<?xml version="1.0" encoding="UTF-8"?><Price><PsgType>CHD</PsgType></Price>
乘客类型:ADT


就这个结果让我想了好久,也曾试着跟进去看看代码的实现,不过恕我本人愚笨,未曾找到,

但就此结果让我甚是想不通。纠结了很久。如果你也想知道答案的话,下篇文章会有答案的,

不过还是希望看到的人能独立思考下。大家可以交流下心得