在 java 中使用 jdom 创建 xml

create xml using jdom in java

我需要使用具有父子关系的嵌套元素的 jdom 创建 xml。我已经编写了代码,但 xml 与我的 requirement.In 评论不匹配 我已经评论了我要求的 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>

@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()));

    }