【问题标题】:How to put String text without converting content to xml file in Java?如何在不将内容转换为 Java 中的 xml 文件的情况下放置字符串文本?
【发布时间】:2011-03-31 13:44:06
【问题描述】:

我需要将字符串内容放入 Java 中的 xml。我使用这种代码在 xml 中插入信息:

Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File ("file.xml"));
DOMSource source = new DOMSource (doc);

Node cards = doc.getElementsByTagName ("cards").item (0);

Element card = doc.createElement ("card");
cards.appendChild(card);

Element question = doc.createElement("question");
question.appendChild(doc.createTextNode("This <b>is</b> a test.");
card.appendChild (question);

StreamResult result = new StreamResult (new File (file));
Transformer tf = TransformerFactory.newInstance().newTransformer();
tf.setOutputProperty(OutputKeys.INDENT, "yes");
tf.transform(source, result);

但是字符串在 xml 中是这样转换的:

<cards>
  <card>
    <question>This &lt;b&gt;is&lt;/b&gt; a test.</question>
  </card>
</cards>

应该是这样的:

<cards>
  <card>
    <question>This <b>is</b> a test.</question>
  </card>
</cards>

我尝试使用 CDDATA 方法,但它的代码如下:

// I changed this code
question.appendChild(doc.createTextNode("This <b>is</b> a test.");
// to this
question.appendChild(doc.createCDATASection("This <b>is</b> a test.");

这段代码得到一个 xml 文件,如下所示:

<cards>
  <card>
    <question><![CDATA[This <b>is</b> a test.]]></question>
  </card>
</cards>

希望有人能帮我把String内容放到xml文件中,内容完全相同。

提前致谢!

【问题讨论】:

  • 确定你真的想在你的b元素中包含一个b元素吗?任何实际读取question 元素内容的内容都会按照您的意图看到文本,实体编码用于将其存储在 XML 数据中。例如,它所做的实际上是正确的,以后读取您的 XML 的任何内容都会得到文本“This is a test”。
  • @Crowder 是的,我确定。我使用 Java 程序创建了卡片组,其中包括带有问答部分的卡片。文本应使用 -tags 设置样式,以便可以在 Flash 中使用样式文本。在 java 程序中,我使用带有 html 内容的 JTextPane,所以我可以正确地看到它。

标签: java xml string dom document


【解决方案1】:

这是预期的行为。

考虑如果括号保持原样,最终结果基本上是:

<cards>  
  <card>    
    <question>
      This 
      <b>
        is
      </b>
       a test.
    </question>  
  </card>
</cards>

基本上,这将导致&lt;b&gt; 成为xml 树中的一个附加节点。将括号编码为&amp;lt;&amp;gt; 可确保在由任何 xml 解析器显示时,括号将被显示,并且不会被混淆为附加节点。

如果您真的希望它们按您说的那样显示,您将需要创建名为 b 的元素。这不仅会很尴尬,而且也不会像您在上面写的那样显示 - 它会显示为额外的嵌套节点,就像我在上面显示的那样。因此,您需要修改 xml 编写器以内联输出这些标签。

讨厌。

【讨论】:

  • +1 这是一个很好的观点,关于 OP 可能需要什么。
【解决方案2】:

查看此解决方案:how to unescape XML in java

【讨论】:

  • 谢谢!这使事情变得容易。 :-D 我将保存的 xml 文件读取为字符串,转换并再次保存。
【解决方案3】:

也许你可以这样解决(代码仅用于&lt;question&gt;标签部分):

Element question = doc.createElement("question");
question.appendChild(doc.createTextNode("This ");
Element b = doc.createElement("b");
b.appendChild(doc.createTextNode("is");
question.appendChild(b);
question.appendChild(doc.createTextNode(" a test.");
card.appendChild(question);

【讨论】:

    【解决方案4】:

    您实际上正在尝试做的是将 XML 插入到 DOM 的中间而不对其进行解析。你不能这样做,因为 DOM API 不支持它。

    您有三个选择:

    • 您可以序列化 DOM,然后在适当的位置插入字符串。最终结果可能是格式正确的 XML,也可能不是……取决于您插入的字符串中的内容。

    • 您可以创建和插入表示文本和&lt;b&gt;...&lt;/b&gt; 元素的DOM 节点。这要求您知道要插入的内容的 XML 结构。 @bluish 的回答举了一个例子。

    • 您可以将字符串包装在某个容器元素中,使用 XML 解析器对其进行解析以提供第二个 DOM,找到感兴趣的节点,并将它们添加到原始 DOM。这要求字符串在包装在容器元素中时是格式正确的 XML。

    【讨论】:

      【解决方案5】:

      或者,既然您已经在使用转换,为什么不一直使用呢?

      <xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      
      <xsl:output omit-xml-declaration="yes"/>
      
      <xsl:template match="@*|node()">
          <xsl:copy>
              <xsl:apply-templates select="@*|node()" />
          </xsl:copy>
      </xsl:template>
      
      <xsl:template match="cards">
          <card>
              <question>This <b>is</b> a test</question>
          </card>
      </xsl:template>
      </xsl:stylesheet>
      

      【讨论】:

        猜你喜欢
        • 2013-08-30
        • 1970-01-01
        • 2015-09-01
        • 2011-04-22
        • 1970-01-01
        • 1970-01-01
        • 2021-01-20
        • 2012-02-28
        相关资源
        最近更新 更多