加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

将String转换为xml元素java

发布时间:2020-12-16 23:31:14 所属栏目:百科 来源:网络整理
导读:我想将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 = Doc
我想将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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读