在Java中将节点列表转换为字符串

在Java中将节点列表转换为字符串,java,xpath,tostring,nodelist,Java,Xpath,Tostring,Nodelist,我正在尝试将NodeList转换为String,这样我就可以用任何我想要的方式来操作它,但我似乎无法在网上找到答案 我尝试将节点列表存储在节点数组中,但打印出来的所有数据都将为空 TextingXPath.java import java.io.IOException; public class TestingXPath { static Node[] copy; static int length; public static void main(String[] args) thro

我正在尝试将NodeList转换为String,这样我就可以用任何我想要的方式来操作它,但我似乎无法在网上找到答案

我尝试将节点列表存储在节点数组中,但打印出来的所有数据都将为空

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();
        }

    }

}
import java.io.IOException;
公共类测试XPath{
静态节点[]复制;
静态整数长度;
公共静态void main(字符串[]args)引发SAXException、IOException、,
ParserConfiguration异常{
URL obj=新URL(“http://jbossews-ashton.rhcloud.com/testXML.jsp");
HttpURLConnection con=(HttpURLConnection)obj.openConnection();
con.setRequestMethod(“GET”);
int responseCode=con.getResponseCode();
如果(响应代码==200){
DocumentBuilderFactory domFactory=DocumentBuilderFactory
.newInstance();
试一试{
DocumentBuilder=domFactory.newDocumentBuilder();
文档dDoc=builder.parse(con.getInputStream());
XPath=XPathFactory.newInstance().newXPath();
Node节点=(Node)xPath.evaluate(
“/HDB/Resident[@Name='Batman']/Preference”,dDoc,
XPathConstants.NODE);
if(null!=节点){
NodeList NodeList=node.getChildNodes();
for(int i=0;null!=nodeList
&&i

}

用于打印
首选项1
元素


使用
System.out.println(nod.getTextContent())

此将节点转换为字符串它以XML的形式获取节点,如果您只需要不带XML的内容,请使用
getTextContent

Node elem = nodeList.item(i);//Your Node
StringWriter buf = new StringWriter();
Transformer xform = TransformerFactory.newInstance().newTransformer();
xform.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); // optional
xform.setOutputProperty(OutputKeys.INDENT, "yes"); // optional
xform.transform(new DOMSource(elem), new StreamResult(buf));
System.out.println(buf.toString()); // your string

您可以按如下方式打印节点名称

System.out.println(node.getNodeName());

尝试System.out.println(nod.getTextContent());您的代码没有显示任何转换为字符串的实际尝试。你到底想转换什么?那里面是什么“…”?为什么你认为字符串比DOM更能让你进行更好的操作?@realpoint The“…”是我试图自己解决问题的地方,但由于它不起作用,我删除了整个代码块。我希望在另一个类中使用NodeList的存储值这只打印“实际文本内容”,即
c
的“c”,但没有任何内部节点,很遗憾。。。