Convert a String to a xml Element java

java, xml

Solution

There is more than one way to parse XML from string:

Example 1:

  String xml = "Your XML";
  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  DocumentBuilder db = dbf.newDocumentBuilder();
  Document doc = db.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));     

Example 2:

Using a SAXParser which can read an inputsource:

 SAXParserFactory factory = SAXParserFactory.newInstance();
 SAXParser saxParser = factory.newSAXParser();
 DefaultHandler handler = new DefaultHandler() {
 saxParser.parse(new InputSource(new StringReader("Your XML")), handler);    

See: SAXParser, InputSource

Problem

I want to convert a String to `org.jdom.Element` ``` String s = "<rdf:Description rdf:about=\"http://dbpedia.org/resource/Barack_Obama\">"; ``` How can I do it?

Original source

Related problems