简体   繁体   English

XML PArsing出现“ [#document:null]”问题

[英]“[#document: null]” issue with XML PArsing

after having a good look online, I am struggling with this one. 在网上看了很开心之后,我正在努力解决这个问题。 Basically, I call an API, it returns XML, this xml requires to be updated, then sent back to the api. 基本上,我调用一个API,它返回XML,此xml需要更新,然后发送回api。 The code I currently have is : 我目前拥有的代码是:

public string update(){

String url = GeneralProps.getProperty("XMLUpdateURL")+testCaseID+"/block/include/data/no";
ArrayList<String> tempArray =  HttpHelper.sendAuthGetRequest(url, GeneralProps.getProperty("Username"), GeneralProps.getProperty("Password"));

        Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new StringReader(tempArray.get(1).toString())));

    XPathExpression expr = xpath.compile("//Step["+ stepNo + "]/"+ nodeName         +"/text()");
        Object result = expr.evaluate(doc, XPathConstants.NODESET);
        NodeList nodes = (NodeList) result;
        for (int i = 0; i < nodes.getLength(); i++) {
            System.out.println(nodes.item(i).getNodeValue());
            nodes.item(i).setNodeValue(newNodeValue);
            System.out.println(nodes.item(i).getNodeValue());

        }

    return doc;

    }

However, doc returns [#document: null] - which means the document is actually there, I can make the amendments, but I cannot seem to get the xml from the doc as this is required to bepassed into another method to upload the xml to the API, but i cannot figure out how!! 但是,doc返回[#document:null]-这意味着文档实际上在那儿,我可以进行修改,但是我似乎无法从doc中获取xml,因为这需要传递给另一种将xml上传到的方法API,但我不知道怎么做!

I figured this out.... 我想通了...

using the below code: 使用以下代码:

Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        StreamResult sResult = new StreamResult(new StringWriter());
        DOMSource source = new DOMSource(doc);
        transformer.transform(source, sResult);
        String xmlString = sResult.getWriter().toString();      

        return xmlString;

I was able to transform the contents to a string and upload the string as I required... 我能够将内容转换为字符串,然后根据需要上传字符串...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM