如何在Java中从字符串创建XML对象?

时间:2021-07-08 19:40:44

I am trying to write a code that helps me to create a XML object. For example, I will give a string as input to a function and it will return me a XMLObject.

我正在尝试编写一个代码,帮助我创建一个XML对象。例如,我将给一个字符串作为函数的输入,它将返回一个XMLObject。

XMLObject convertToXML(String s) {}

When I was searching on the net, generally I saw examples about creating XML documents. So all the things I saw about creating an XML and write on to a file and create the file. But I have done something like that:

当我在网上搜索时,我通常会看到关于创建XML文档的示例。我看到的创建XML,写入文件,创建文件的所有事情。但我做过这样的事:

Document document = new Document();
Element child = new Element("snmp");
child.addContent(new Element("snmpType").setText("snmpget"));
child.addContent(new Element("IpAdress").setText("127.0.0.1"));
child.addContent(new Element("OID").setText("1.3.6.1.2.1.1.3.0"));
document.setContent(child);

Do you think it is enough to create an XML object? and also can you please help me how to get data from XML? For example, how can I get the IpAdressfrom that XML?

您认为创建XML对象就足够了吗?你能帮我从XML中获取数据吗?例如,如何从该XML获得ipadressr ?

Thank you all a lot

非常感谢大家

EDIT 1: Actually now I thought that maybe it would be much easier for me to have a file like base.xml, I will write all basic things into that for example:

编辑1:实际上现在我想,也许我有一个像base这样的文件会容易得多。xml,我将把所有基本的东西都写进去,例如:

<snmp>
<snmpType><snmpType>
<OID></OID>
</snmp>

and then use this file to create a XML object. What do you think about that?

然后使用这个文件创建一个XML对象。你怎么看?

2 个解决方案

#1


73  

If you can create a string xml you can easily transform it to the xml document object e.g. -

如果您可以创建一个字符串xml,您可以很容易地将其转换为xml文档对象。

String xmlString = "<?xml version=\"1.0\" encoding=\"utf-8\"?><a><b></b><c></c></a>";  

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();  
DocumentBuilder builder;  
try {  
    builder = factory.newDocumentBuilder();  
    Document document = builder.parse(new InputSource(new StringReader(xmlString)));  
} catch (Exception e) {  
    e.printStackTrace();  
} 

You can use the document object and xml parsing libraries or xpath to get back the ip address.

您可以使用文档对象和xml解析库或xpath来获取ip地址。

#2


10  

try something like

试着像

public static Document loadXML(String xml) throws Exception
{
   DocumentBuilderFactory fctr = DocumentBuilderFactory.newInstance();
   DocumentBuilder bldr = fctr.newDocumentBuilder();
   InputSource insrc = new InputSource(new StringReader(xml));
   return bldr.parse(insrc);
}

#1


73  

If you can create a string xml you can easily transform it to the xml document object e.g. -

如果您可以创建一个字符串xml,您可以很容易地将其转换为xml文档对象。

String xmlString = "<?xml version=\"1.0\" encoding=\"utf-8\"?><a><b></b><c></c></a>";  

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();  
DocumentBuilder builder;  
try {  
    builder = factory.newDocumentBuilder();  
    Document document = builder.parse(new InputSource(new StringReader(xmlString)));  
} catch (Exception e) {  
    e.printStackTrace();  
} 

You can use the document object and xml parsing libraries or xpath to get back the ip address.

您可以使用文档对象和xml解析库或xpath来获取ip地址。

#2


10  

try something like

试着像

public static Document loadXML(String xml) throws Exception
{
   DocumentBuilderFactory fctr = DocumentBuilderFactory.newInstance();
   DocumentBuilder bldr = fctr.newDocumentBuilder();
   InputSource insrc = new InputSource(new StringReader(xml));
   return bldr.parse(insrc);
}