45

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.

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:

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?

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:

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

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

Belphegor
  • 4,456
  • 11
  • 34
  • 59
Ozer
  • 1,245
  • 4
  • 20
  • 27
  • give the fully qualified name (FQN) of XMLObject. Are you using xmlbeans? – Aravind Yarram Sep 30 '11 at 07:47
  • Ok, first question: What is `XMLObject`? Which library is that? – forty-two Sep 30 '11 at 07:49
  • XMLObject is just an example, I want a function that gets a string as a parameter and then it will return a XML object . I am using jdom for implementation. – Ozer Sep 30 '11 at 07:55
  • What do you want to do with XMLObject once you have it? If you just want a representation that you can manipulate in code and navigate through, then Document/Element are probably good enough. – Richard Sep 30 '11 at 08:14
  • @Richard I will create XML objects and will send them through sockets and then I will get the informations that are written in XML. Do you think Document/Element is enough ? – Ozer Sep 30 '11 at 08:26

2 Answers2

86

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

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.

Belphegor
  • 4,456
  • 11
  • 34
  • 59
Jayendra
  • 52,349
  • 4
  • 80
  • 90
  • as I understand "parse" returns a DOM document object, what will you do to get a SAX version of this code? thanx! – theexplorer Apr 17 '15 at 21:49
  • 1
    What if there is no source, and we want to create a new Document object and then append children to it , Is it possible or do we have to create the root Element first? – Romantic Electron Apr 29 '15 at 14:25
  • 1
    just copy-pasted and spent a pair of minutes to check because I used wrong imports to go too fast, double check, correct imports are: import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.xml.sax.InputSource; – Falco Apr 16 '20 at 15:08
11

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);
}
i100
  • 4,529
  • 1
  • 22
  • 20