简体   繁体   English

DOM XML:写入文件并再次读取

[英]DOM XML: Write to file and read it again

I'm generating XML file using DOM. 我正在使用DOM生成XML文件。

public void save() 
{
    if(readonly)
    {
        throw new WritingToLockedFileException();
    }
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer;
    try {
        transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(fileInfo);
        StreamResult result = new StreamResult(fileXML);
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,"yes"); 
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        transformer.transform(source, result);
        System.out.println("File saved!");
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }
}

When I'm using 当我使用

transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,"yes"); 
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

then I'm getting [#text, lock, #text, testing, #text] instead of [lock, testing] which I get, when I comment those 3 lines. 当我评论这3行时,我得到的是[#text,lock,#text,testing,#text],而不是[lock,testing]。 Does anybody know how can i generate human-readable XML file, that can be re-readed by the DOM parser. 有谁知道我如何生成人类可读的XML文件,该文件可以由DOM解析器重新读取。

That list I'm getting using this function: 我正在使用此功能的列表:

public List<String> getTags() 
{
    Element summary = (Element) fileInfo.getElementsByTagName("summary").item(0);
    Element tags = (Element) summary.getElementsByTagName("tags").item(0);
    NodeList list = tags.getChildNodes();
    List<String> taglist = new ArrayList<String>();
    for(int i=0; i<list.getLength(); i++)
    {
        taglist.add(list.item(i).getNodeName());
    }
    return taglist;
}

and the XML human-readable xml: 和XML可读XML:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<summary>
    <tags>
        <lock/>
        <testing/>
    </tags>
</summary>

What you see as #text nodes are just text spaces between tags (new lines and indentations). 您所看到的#text节点只是标记(新行和缩进)之间的文本空间。 In general you might want to use these text blocks in some way (if there is more than just spaces). 通常,您可能希望以某种方式使用这些文本块(如果不仅仅是空格)。 But as long as you don't need to use them in your case just add skipping check for these nodes into the for loop forming taglist: 但是只要您不需要使用它们,只需将这些节点的跳过检查添加到for循环中形成标记列表即可:

    for (int i=0; i<list.getLength(); i++) {
        org.w3c.dom.Node elem = list.item(i);
        if (elem.getNodeType() == org.w3c.dom.Node.TEXT_NODE && 
                elem.getNodeValue().trim().isEmpty()) {
            continue;
        }
        taglist.add(elem.getNodeName());
    }

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

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