在Java中,如何在不将内容转换为xml文件的情况下放置字符串文本?

在Java中,如何在不将内容转换为xml文件的情况下放置字符串文本?,java,xml,string,dom,document,Java,Xml,String,Dom,Document,我需要用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

我需要用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);
Document doc=DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(新文件(“File.xml”);
DOMSource=新的DOMSource(doc);
节点卡片=doc.getElementsByTagName(“卡片”)。项(0);
元素卡片=doc.createElement(“卡片”);
卡片。附属物(卡片);
元素问题=doc.createElement(“问题”);
appendChild(doc.createTextNode(“这是一个测试”);
儿童卡(问题);
StreamResult结果=新的StreamResult(新文件(文件));
Transformer tf=TransformerFactory.newInstance().newTransformer();
tf.setOutputProperty(OutputKeys.INDENT,“是”);
变换(源、结果);
但字符串在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>
// 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.");

本bis/b是一项测试。
应该是这样的:

<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>
// 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.");

这是一个测试。
我尝试使用CDDATA方法,但它的代码如下所示:

<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>
// 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.");
//我更改了这个代码
appendChild(doc.createTextNode(“这是一个测试”);
//对此
appendChild(doc.createCDATA节(“这是一个测试”);
此代码获取的xml文件如下所示:

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

这是一个测试。]]>
我希望有人能帮助我在xml文件中使用完全相同的内容放置字符串内容


提前感谢!

也许您可以用这种方式解决它(代码仅适用于
标记部分):


也许您可以用这种方式解决它(代码仅用于
标记部分):


这是意料之中的行为

考虑一下,如果括号按您所说的方式保留,最终结果基本上是:

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

这
是
测试。
基本上,这将导致
成为xml树中的一个附加节点。将括号编码为
可确保在任何xml解析器显示时,括号都会显示出来,而不会混淆为附加节点

如果您真的想让它们按您所说的那样显示,那么您需要创建名为
b
的元素。这不仅会很尴尬,而且也不会像您上面所写的那样显示-它会显示为我上面所示的其他嵌套节点。因此,您需要修改xml编写器以内联输出ose标签


讨厌。

这是意料之中的行为

考虑一下,如果括号按您所说的方式保留,最终结果基本上是:

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

这
是
测试。
基本上,这将导致
成为xml树中的一个附加节点。将括号编码为
可确保在任何xml解析器显示时,括号都会显示出来,而不会混淆为附加节点

如果您真的想让它们按您所说的那样显示,那么您需要创建名为
b
的元素。这不仅会很尴尬,而且也不会像您上面所写的那样显示-它会显示为我上面所示的其他嵌套节点。因此,您需要修改xml编写器以内联输出ose标签


讨厌。

检查此解决方案:

检查此解决方案:

您实际尝试的是在不解析的情况下将XML插入DOM的中间。由于DOM API不支持它,因此无法执行此操作

你有三个选择:

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

  • 您可以创建和插入表示文本和
    元素的DOM节点。这要求您知道要插入的内容的XML结构。@bluish的回答给出了一个示例

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


实际上,您要做的是在不解析XML的情况下将XML插入DOM的中间。因为DOM API不支持XML,所以您无法这样做

你有三个选择:

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

  • 您可以创建和插入表示文本和
    元素的DOM节点。这要求您知道要插入的内容的XML结构。@bluish的回答给出了一个示例

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


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

<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>

这是一个测试

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

<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>

这是一个测试

是否确实要在
问题
元素中实际包含
b
元素?任何实际读取
问题
元素内容的内容都将按您的意愿看到文本,实体编码用于将其存储在XML数据中。例如,它所做的操作实际上是正确的,任何读取X的操作都是正确的ML稍后将得到文本“这是一个测试”。@Crowder是的,我确定。我使用Java程序创建了一个包含问题和答案的卡片组