如何在Java中创建一个Document对象?
我想用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所见,您正在创建一个 Document 对象,并将节点添加到
header
和body
节点,但这些节点不会添加到您的 Document 对象实例<代码>文档。我相信您希望将这些节点添加到
root
元素中,该元素已添加到您的document
中。因此,您可以将其添加到文档的根目录中,如下所示:
From what I can see, you are creating a Document object, and adding nodes to the
header
andbody
nodes, but these nodes are not being added to your Document object instancedocument
.I believe you would want to add these nodes to the
root
element, which is already added to yourdocument
.So, you could add it to your document's root as shown below: