在Java中将String转换为XML doc

时间:2014-07-04 12:50:44

标签: java xml string

您好我已经将XML内容生成为字符串。但我必须将字符串转换为XML数据类型。

String str = "<?xml version="1.0" encoding="utf-8"?><Message><Rc1>12343322</Rc1><Rc2>125145552</Rc2><Rc3>54682242</Rc3><Rc4>7777332287</Rc4></Message>"

如何将此字符串转换为xmldoc类型?

1 个答案:

答案 0 :(得分:0)

  String xmlStr = "<?xml version=\"1.0\" encoding=\"utf-8\"?><Message><Rc1>12343322</Rc1>     <Rc2>125145552</Rc2><Rc3>54682242</Rc3><Rc4>7777332287</Rc4></Message>"
 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();  
  DocumentBuilder builder = factory.newDocumentBuilder();  
  Document doc = builder.parse( new InputSource( new StringReader( xmlStr ) ) );