简体   繁体   English

如何将XML内容作为String获取

[英]How to get XML content as a String

<root>
<h id="1">
    <d value="1,2,3,4,5"><open>10:00</open><close>23:00</close></d>
    <d value="6"><open>10:00</open><close>2:00</close></d>
    <d value="7"><open>10:00</open><close>21:00</close></d>
</h>
<h id="2">
</h>
</root>

Here I have the XML which root has list of <h> tagged nodes. 在这里,我有XML,其中root具有<h>标记节点的列表。 Now I need to break these into parts and set it into different variables (add into a map). 现在我需要将这些分成几部分并将其设置为不同的变量(添加到地图中)。

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(new InputSource(new ByteArrayInputStream(data.getBytes("utf-8"))));
NodeList nList = doc.getElementsByTagName("h");
for (int i = 0; i < nList.getLength(); i++)
{
    Node nNode = nList.item(i);
    System.out.println(nNode.getAttributes().getNamedItem("id") + " " + ?????);        
}

what should I call in order to get the value ( String value ) of a nNode ? 我应该调用什么来获取nNode的值( String值 )?

Here is what Im looking for as the asnwer for the above code once some one fills the ???? 这是我在寻找上述代码的asnwer,一旦有人填写了????

1 <h id="1"><d value="1,2,3,4,5"><open>10:00</open><close>23:00</close></d><d value="6">open>10:00</open><close>2:00</close></d><d value="7"><open>10:00</open><close>21:00</close></d></h>
2 <h id="2"></h>

And i don't mind having as root element 我不介意作为根元素

You can use Node.getTextContent() to conveniently get all the text of a node (gets text of children as well). 您可以使用Node.getTextContent()方便地获取节点的所有文本(也可以获取子文本)。

See Parsing xml file contents without knowing xml file structure for a short example. 有关简短示例,请参阅解析xml文件内容,而不知道xml文件结构


If you're trying to get the value attributes of the d nodes (I can't actually tell, your question is slightly unclear to me), then it would be different -- for that you would iterate through the children of each h node (use getChildNodes() or getFirstChild() + getNextSibling() ) then grab their value attributes just as you are getting the id attribute of the h nodes (the above link also shows an example of iterating through child nodes). 如果您正在尝试获取d节点的value属性(我实际上无法告诉您,您的问题对我来说有点不清楚),那么它会有所不同 - 因为您将迭代每个h节点的子节点(使用getChildNodes()getFirstChild() + getNextSibling() )然后获取它们的value属性,就像获取h节点的id属性一样(上面的链接也显示了迭代子节点的示例)。

Have you tried jDom library? 你试过jDom库吗? http://www.jdom.org/docs/apidocs/org/jdom2/output/XMLOutputter.html http://www.jdom.org/docs/apidocs/org/jdom2/output/XMLOutputter.html

 XMLOutputter outp = new XMLOutputter();
 String s = outp.outputString(your_jdom_element);

如果您使用的是来自javax.xml.soap.Node Node,您是否尝试过nNode.toString()

You can use that: 你可以用它:

http://docs.oracle.com/javase/7/docs/api/org/w3c/dom/Node.html#getTextContent() http://docs.oracle.com/javase/7/docs/api/org/w3c/dom/Node.html#getTextContent()

but your sample nNode has other nodes, not just text. 但是你的样本nNode有其他节点,而不仅仅是文本。 It seems you need helper method to construct String from child nodes. 看来你需要帮助方法从子节点构造String。

Pass your nNode to nodeToString 将nNode传递给nodeToString

XML Node to String in Java Java中的XML节点到字符串

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

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