简体   繁体   English

从xml字符串构建DOM文档给我一个空文档

[英]Building DOM document from xml string gives me a null document

I'm trying to use the DOM library to parse a string in xml format. 我正在尝试使用DOM库来解析xml格式的字符串。 For some reason my document contains nulls and I run into issues trying to parse it. 由于某种原因,我的文档包含空值,并且在尝试解析它时遇到了问题。 The string variable 'response' is not null and I am able to see the string when in debug mode. 字符串变量'response'不为null,在调试模式下,我能够看到该字符串。

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        InputSource is = new InputSource(new StringReader(response));
        Document doc = builder.parse(is);

        NodeList nodes = doc.getElementsByTagName("BatchFile");;
        for (int i = 0; i < nodes.getLength(); i++) {
            Element element = (Element) nodes.item(i);

            NodeList batchItem = element.getChildNodes();
            String uri = batchItem.item(0).getNodeValue();
            String id = batchItem.item(1).getNodeValue();
            String fqName = batchItem.item(2).getNodeValue();
          }

Highlighting over the line Document doc = builder.parse(is); 突出显示行Document doc = builder.parse(is); after it has run shows the result of [#document: null] . 运行后显示[#document: null]的结果。

Edit: I've managed to not got an empty doc now but the string values are still null (at end of code). 编辑:我设法现在没有空文档,但字符串值仍然为null(在代码末尾)。 How would I get the value of something like this 我将如何获得像这样的东西的价值

        <GetBatchFilesResult>
            <BatchFile>
                <Uri>uri</Uri>
                <ID>id</ID>
                <FQName>file.zip</FQName>
            </BatchFile>

        </GetBatchFilesResult>

You can also use getTextContent(). 您也可以使用getTextContent()。 getNodeValue will return null for elements. getNodeValue将为元素返回null。 Besides, you'd better use getElementsByTagName, since white spaces are also treated as one of the child nodes. 此外,您最好使用getElementsByTagName,因为空格也被视为子节点之一。

Element element = (Element) nodes.item(i);
String uri = element.getElementsByTagName("Uri").item(0).getTextContent();
String id =  element.getElementsByTagName("ID").item(0).getTextContent();
String fqName =  element.getElementsByTagName("FQName").item(0).getTextContent();

Check Node API document to see what type of nodes will return null for getNodeValue. 查看Node API文档以查看哪种类型的节点将为getNodeValue返回null。

I found the solution. 我找到了解决方案。 Seems stupid that you have to do it this way to get a value from a node. 似乎很愚蠢,您必须采用这种方式才能从节点获取值。

        Element element = (Element) nodes.item(i);

        NodeList batchItem = element.getChildNodes();
        Element uri = (Element) batchItem.item(0);
        Element id = (Element) batchItem.item(1);
        Element fqName = (Element) batchItem.item(2);
        NodeList test = uri.getChildNodes();
        NodeList test1 = id.getChildNodes();
        NodeList test2 = fqName.getChildNodes();

        String strURI= test.item(0).getNodeValue();
        String strID= test1.item(0).getNodeValue();
        String strFQName= test2.item(0).getNodeValue();

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

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