将String转换为xml元素java

前端之家收集整理的这篇文章主要介绍了将String转换为xml元素java前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想将String转换为org.jdom.Element

String s = "<rdf:Description rdf:about=\"http://dbpedia.org/resource/Barack_Obama\">";

我该怎么做?

解决方法

从字符串解析XML有多种方法

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

使用可以读取输入源的SAXParser:

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

见:SAXParser,InputSource

猜你在找的XML相关文章