在java中使用jdom创建xml

时间:2016-10-26 06:25:47

标签: java xml jdom

我需要使用带有父子关系的嵌套元素的jdom创建xml。我写了代码,但xml与我的要求不符。在评论中,我评论了我所需的xml格式。请帮帮我..

我的代码是

public static void main(String[] args) throws IOException {

    Map<String, String> map = new LinkedHashMap();
    map.put("webport", "/");
    map.put("webserverobsoluteurl", "https://");
    map.put("logger.folder", "/opt/apihandler/logs/wsapi");
    map.put("logger.port", "9001");
    map.put("logger.allowedlevel", "6");
    map.put("logpropsfile", "/opt/apihandler/WSAPIHandler/WEB-INF/log4j.properties");
    System.out.println(map);

    Element root = new Element("worldsmart");
    Document doc = new Document();
    Element pchild = null;

    for (Map.Entry<String, String> entry : map.entrySet()) {
        System.out.println(entry.getKey() + "=====================" + entry.getValue());

        if (entry.getKey().contains(".")) {

            int dotIndex = entry.getKey().indexOf(".");
            String parentTag = entry.getKey().substring(0, dotIndex);
            String childTag = entry.getKey().substring(dotIndex + 1, entry.getKey().length());
            pchild = new Element(parentTag);
            pchild.addContent(new Element(childTag).setText(entry.getValue()));
            root.addContent(pchild);

        } else {
            Element child = new Element(entry.getKey());
            child.addContent(entry.getValue());
            root.addContent(child);
        }
    }
    doc.setRootElement(root);
    XMLOutputter xmloutput = new XMLOutputter();
    xmloutput.setFormat(Format.getPrettyFormat());
    xmloutput.output(doc, new FileWriter("/root/Desktop/abc.xml"));
    System.out.println("file saved");
}

必需的输出:

<worldsmart>
    <webserverobsoluteurl>https://</webserverobsoluteurl>
    <webport>/</webport>
    <logger>
        <folder>/opt/apihandler/logs/wsapi</folder>
        <port>9001</port>
        <allowedlevel>6</allowedlevel>
    </logger>
    <logpropsfile> <![CDATA[ /opt/apihandler/WSAPIHandler/WEB-INF/log4j.properties ]]> </logpropsfile>
</worldsmart>

但是我将XML文件作为

<worldsmart>
    <webport>/</webport>
    <webserverobsoluteurl>https://</webserverobsoluteurl>
    <logger>
        <folder>/opt/apihandler/logs/wsapi</folder>
    </logger>
    <logger>
        <port>9001</port>
    </logger>
    <logger>
        <allowedlevel>6</allowedlevel>
    </logger>
    <logpropsfile>/opt/apihandler/WSAPIHandler/WEB-INF/log4j.properties</logpropsfile>
</worldsmart>

1 个答案:

答案 0 :(得分:1)

@conscells在评论中对你的循环中的问题有正确的想法 - 你每次在循环中创建一个新的logger元素,而不是重用logger如果它已经存在(并在循环中第一次创建它)。

您的代码:

   if (entry.getKey().contains(".")) {

        int dotIndex = entry.getKey().indexOf(".");
        String parentTag = entry.getKey().substring(0, dotIndex);
        String childTag = entry.getKey().substring(dotIndex + 1, entry.getKey().length());
        pchild = new Element(parentTag);
        pchild.addContent(new Element(childTag).setText(entry.getValue()));
        root.addContent(pchild);

    }

应该有条件首先检查pchild:

   if (entry.getKey().contains(".")) {

        int dotIndex = entry.getKey().indexOf(".");
        String parentTag = entry.getKey().substring(0, dotIndex);
        String childTag = entry.getKey().substring(dotIndex + 1, entry.getKey().length());

        // locate the previously created `logger` element, if any.
        pchild = root.getChild(parentTag);
        if (pchild == null) {

            // need to add the child if it did not exist.
            pchild = new Element(parentTag);
            root.addContent(pchild);
        }
        pchild.addContent(new Element(childTag).setText(entry.getValue()));

    }