我正在尝试将NodeList转换为字符串,以便可以按照自己的意愿进行操作,但是我似乎找不到在线答案。
我尝试将NodeList存储在节点数组中,但是打印出来的所有数据都将为空。
TextingXPath.java
import java.io.IOException;
public class TestingXPath {
static Node[] copy;
static int length;
public static void main(String[] args) throws SAXException, IOException,
ParserConfigurationException {
URL obj = new URL("http://jbossews-ashton.rhcloud.com/testXML.jsp");
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
int responseCode = con.getResponseCode();
if (responseCode == 200) {
DocumentBuilderFactory domFactory = DocumentBuilderFactory
.newInstance();
try {
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document dDoc = builder.parse(con.getInputStream());
XPath xPath = XPathFactory.newInstance().newXPath();
Node node = (Node) xPath.evaluate(
"/HDB/Resident[@Name='Batman ']/Preference", dDoc,
XPathConstants.NODE);
if (null != node) {
NodeList nodeList = node.getChildNodes();
for (int i = 0; null != nodeList
&& i < nodeList.getLength(); i++) {
Node nod = nodeList.item(i);
if (nod.getNodeType() == Node.ELEMENT_NODE)
{
...
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}