首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XML文档的“[# PArsing : null]”问题

XML文档的“[# PArsing : null]”问题
EN

Stack Overflow用户
提问于 2015-04-28 17:37:45
回答 1查看 351关注 0票数 0

在网上看了一眼之后,我正在努力解决这个问题。基本上,我调用一个API,它返回XML,这个xml需要更新,然后发送回api。我目前拥有的代码是:

代码语言:javascript
复制
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 -这意味着文档实际上在那里,我可以进行修改,但我似乎无法从文档中获取xml,因为这需要传递到另一个方法中以将xml上载到API,但我不知道如何!!

EN

回答 1

Stack Overflow用户

发布于 2015-04-28 21:20:51

我想通了..。

使用以下代码:

代码语言:javascript
复制
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;

我能够将内容转换为字符串,并根据需要上传字符串……

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29915695

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档