简体   繁体   English

字符串到java中的dom文档

[英]string to a dom document in java

I have a string which has an xml inside. 我有一个内部有xml的字符串。 I want to write this to a Document in java so i can later retrieve the attributes within it and store them in other strings. 我想将它写入java中的Document ,以便稍后检索其中的属性并将它们存储在其他字符串中。 Could someone provide me with an example of this please? 有人能为我提供一个这样的例子吗?

Some more details are necessary for a proper answer but you can start with the DocumentBuilder like this: 更多细节是正确答案所必需的,但您可以从DocumentBuilder开始,如下所示:

  DocumentBuilder db = DocumentBuilderFactory.newDocumentBuilder()
  Document doc = db.parse(yourstring);

You now have a Document from which you can access your XML attributes etc. 您现在拥有一个Document,您可以从中访问XML属性等。

http://www.java-samples.com/showtutorial.php?tutorialid=152 http://www.java-samples.com/showtutorial.php?tutorialid=152

This is a good example of how to parse out some XML. 这是如何解析一些XML的一个很好的例子。 This should be a good start for you. 这应该是一个很好的开始。

Document d = DocumentBuilderFactory.newInstance()。newDocumentBuilder()。parse(new ByteArrayInputStream(myXMLString.getBytes());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM