簡體   English   中英

XML PArsing出現“ [#document:null]”問題

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

在網上看了很開心之后,我正在努力解決這個問題。 基本上,我調用一個API,它返回XML,此xml需要更新,然后發送回api。 我目前擁有的代碼是:

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;

    }

但是,doc返回[#document:null]-這意味着文檔實際上在那兒,我可以進行修改,但是我似乎無法從doc中獲取xml,因為這需要傳遞給另一種將xml上傳到的方法API,但我不知道怎么做!

我想通了...

使用以下代碼:

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;

我能夠將內容轉換為字符串,然后根據需要上傳字符串...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM