如何在Java中创建一个Document对象?

发布于 2024-12-07 19:22:44 字数 1993 浏览 1 评论 0原文

我想用 jdom 创建一个 Document 对象。我已经编写了一个函数,但是在调试后我可以看到它没有创建。由于我是 XML 新手,我不明白为什么我不能创建。你能帮我吗?

public Document createSNMPMessage (){

    Element root = new Element("message");
    Document document = new Document(root);

    Element header = new Element("header");

    Element messageType = new Element("messageType").setText("snmp");
    Element sendFrom = new Element("sendFrom").setText("192.168.0.16");
    Element hostName = new Element("hostName").setText("oghmasysMehmet");
    Element sendTo = new Element("sendTo").setText("192.168.0.12");
    Element receiverName = new Element("receiverName").setText("Mehmet");
    Element date = new Element("date").setText("03/10/2011");

    header.addContent(messageType);
    header.addContent(sendFrom);
    header.addContent(hostName);
    header.addContent(sendTo);
    header.addContent(receiverName);
    header.addContent(date);

    Element body = new Element("body");

    Element snmpType = new Element("snmpType").setText("getbulk");
    Element ip = new Element("ip").setText("127.0.0.1");
    Element port = new Element("port").setText("161");
    Element oids = new Element("oids");
    Element oid = new Element("oid").setText("1.3.6.1.2.1.1.3.0");
    oids.addContent(oid);
    Element community = new Element("community").setText("community");
    Element nR = new Element("nR").setText("0");
    Element mR = new Element("mR").setText("5");

    body.addContent(snmpType);
    body.addContent(ip);
    body.addContent(port);
    body.addContent(oids);
    body.addContent(community);
    body.addContent(nR);
    body.addContent(mR);

    return document;

}

当我创建它时,我使用该函数将其转换为字符串;

    public String xmlToString(Document doc) {
    XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
    return outputter.outputString(doc);
}

当我尝试转换为字符串以查看文档内部的内容时,我得到;

<?xml version="1.0" encoding="UTF-8"?>
<message />

I want to create a Document object with jdom. I have written a function but after I debug I can see that it is not created. and since I am new to XML I don't understand why I can not create. Can you please help me for that?

public Document createSNMPMessage (){

    Element root = new Element("message");
    Document document = new Document(root);

    Element header = new Element("header");

    Element messageType = new Element("messageType").setText("snmp");
    Element sendFrom = new Element("sendFrom").setText("192.168.0.16");
    Element hostName = new Element("hostName").setText("oghmasysMehmet");
    Element sendTo = new Element("sendTo").setText("192.168.0.12");
    Element receiverName = new Element("receiverName").setText("Mehmet");
    Element date = new Element("date").setText("03/10/2011");

    header.addContent(messageType);
    header.addContent(sendFrom);
    header.addContent(hostName);
    header.addContent(sendTo);
    header.addContent(receiverName);
    header.addContent(date);

    Element body = new Element("body");

    Element snmpType = new Element("snmpType").setText("getbulk");
    Element ip = new Element("ip").setText("127.0.0.1");
    Element port = new Element("port").setText("161");
    Element oids = new Element("oids");
    Element oid = new Element("oid").setText("1.3.6.1.2.1.1.3.0");
    oids.addContent(oid);
    Element community = new Element("community").setText("community");
    Element nR = new Element("nR").setText("0");
    Element mR = new Element("mR").setText("5");

    body.addContent(snmpType);
    body.addContent(ip);
    body.addContent(port);
    body.addContent(oids);
    body.addContent(community);
    body.addContent(nR);
    body.addContent(mR);

    return document;

}

When I create it, I convert it to string by using that function ;

    public String xmlToString(Document doc) {
    XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
    return outputter.outputString(doc);
}

and When I try to convert to string to see what is inside of the Document, i get;

<?xml version="1.0" encoding="UTF-8"?>
<message />

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神经大条 2024-12-14 19:22:44

据我所见,您正在创建一个 Document 对象,并将节点添加到 headerbody 节点,但这些节点不会添加到您的 Document 对象实例<代码>文档。

我相信您希望将这些节点添加到 root 元素中,该元素已添加到您的 document 中。

因此,您可以将其添加到文档的根目录中,如下所示:

public Document createSNMPMessage (){

    Element root = new Element("message");
    Document document = new Document(root);

    Element header = new Element("header");

    ...
    ...

    Element body = new Element("body");

    ...
    ...

    root.addContent(header);  // NOTE THESE NEW LINES
    root.addContent(body);  // NOTE THESE NEW LINES

    return document;

}

From what I can see, you are creating a Document object, and adding nodes to the header and body nodes, but these nodes are not being added to your Document object instance document.

I believe you would want to add these nodes to the root element, which is already added to your document.

So, you could add it to your document's root as shown below:

public Document createSNMPMessage (){

    Element root = new Element("message");
    Document document = new Document(root);

    Element header = new Element("header");

    ...
    ...

    Element body = new Element("body");

    ...
    ...

    root.addContent(header);  // NOTE THESE NEW LINES
    root.addContent(body);  // NOTE THESE NEW LINES

    return document;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文