Convert string to java xml element

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 this?

+5
source share
3 answers

There are several ways to parse XML from a 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 SAXParser, which can read a data source:

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

See: SAXParser , InputSource

+3
source

(You mentioned the package org.jdom, so I assume you are working with JDOM 1.1.)

0
source

Simple XML Framework http://simple.sourceforge.net/.With , . , .

-1
source

All Articles