简体   繁体   English

Java中的XML节点到字符串

[英]XML Node to String in Java

I came across this piece of Java function to convert an XML node to a Java String representation: 我遇到了这个Java函数来将XML节点转换为Java String表示:

private String nodeToString(Node node) {
StringWriter sw = new StringWriter();
try {
 Transformer t = TransformerFactory.newInstance().newTransformer();
 t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
 t.setOutputProperty(OutputKeys.INDENT, "yes");
 t.transform(new DOMSource(node), new StreamResult(sw));
} catch (TransformerException te) {
 System.out.println("nodeToString Transformer Exception");
}
return sw.toString();
}

It looks straightforward in that it wants the output string doesn't have any XML declaration and it must contain indentation. 它看起来很简单,因为它希望输出字符串没有任何XML声明,并且它必须包含缩进。

But I wonder how the actual output should be, suppose I have an XML node: 但我想知道实际输出应该如何,假设我有一个XML节点:

<p><media type="audio" id="au008093" rights="wbowned">
<title>Bee buzz</title>
</media>Most other kinds of bees live alone instead of in a colony. These bees make
        tunnels in wood or in the ground. The queen makes her own nest.</p>

Could I assume the resulting String after applying the above transformation is: 我可以假设在应用上述转换后生成的String是:

"media type="audio" id="au008093" rights="wbowned" title Bee buzz title /media"

I want to test it myself, but I have no idea on how to represent this XML node in the way this function actually wants. 我想自己测试一下,但我不知道如何以这个函数实际需要的方式表示这个XML节点。

I am bit confused, and thanks in advance for the generous help. 我有点困惑,并提前感谢慷慨的帮助。

All important has already been said. 所有重要的事情都已经说过了。 I tried to compile the following code. 我试着编译以下代码。


import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Node;

public class Test {

  public static void main(String[] args) throws Exception {

    String s = 
      "<p>" +
      "  <media type=\"audio\" id=\"au008093\" rights=\"wbowned\">" +
      "    <title>Bee buzz</title>" +
      "  " +
      "  Most other kinds of bees live alone instead of in a colony." +
      "  These bees make tunnels in wood or in the ground." +
      "  The queen makes her own nest." +
      "</p>";
    InputStream is = new ByteArrayInputStream(s.getBytes());

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document d = db.parse(is);

    Node rootElement = d.getDocumentElement();
    System.out.println(nodeToString(rootElement));

  }

  private static String nodeToString(Node node) {
    StringWriter sw = new StringWriter();
    try {
      Transformer t = TransformerFactory.newInstance().newTransformer();
      t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
      t.setOutputProperty(OutputKeys.INDENT, "yes");
      t.transform(new DOMSource(node), new StreamResult(sw));
    } catch (TransformerException te) {
      System.out.println("nodeToString Transformer Exception");
    }
    return sw.toString();
  }

}

And it produced the following output: 它产生了以下输出:


<p>  <media id="au008093" rights="wbowned" type="audio">    <title>Bee buzz</title>  </media>  Most other kinds of bees live alone instead of in a colony.  These bees make tunnels in wood or in the ground.  The queen makes her own nest.</p>

You can further tweak it by yourself. 你可以自己进一步调整它。 Good luck! 祝好运!

You have an XML respesentation in a DOM tree. 您在DOM树中有XML重新表示。
For example you have opened an XML file and you have passed it in the DOM parser. 例如,您已经打开了一个XML文件,并且已经在DOM解析器中传递了它。
As a result a DOM tree in memory with your XML is created. 结果,创建了内存中包含XML的DOM树。
Now you can only access the XML info via traversal of the DOM tree. 现在,您只能通过遍历DOM树来访问XML信息。
If you need though, a String representation of the XML info of the DOM tree you use a transformation. 但是,如果需要,可以使用DOM树的XML信息的String表示形式来进行转换。
This happens since it is not possible to get the String representation directly from a DOM tree. 发生这种情况是因为无法直接从DOM树获取String表示。
So if for example as Node node you pass in nodeToString is the root element of the XML doc then the result is a String containing the original XML data. 因此,例如,如果您在nodeToString传递的Node node是XML文档的根元素,则结果是包含原始XML数据的String。
The tags will still be there. 标签仍然存在。 Ie you will have a valid XML representation. 即您将拥有有效的XML表示。 Only this time will be in a String variable. 只有这一次才会在String变量中。

For example: 例如:

  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  DocumentBuilder parser = factory.newDocumentBuilder();
  Document xmlDoc = parser.parse(file);//file has the xml
  String xml = nodeToString(xmlDoc.getDocumentElement());//pass in the root
  //xml has the xml info. E.g no xml declaration. Add it
  xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?> + xml;//bad to append this way...
  System.out.println("XML is:"+xml);

DISCLAIMER: Did not even attempt to compile code. 免责声明:甚至没有尝试编译代码。 Hopefully you understand what you have to do 希望你明白你必须做什么

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

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