2015-11-06 38 views
1

的XML時,添加標籤之間的信息我想創建一個看起來像這樣的XML文件:如何創建與Java

<fml-apml> 
<bml> 
    <speech id="s1" start="0.0" language="english" voice="openmary" type="SAPI4" text=""> 
     <description level="1" type="xxx"> 
      <reference>tmp/from-fml-apml.pho</reference> 
     </description> 

     <tm id="tm1"/> 
      TEXT 1 
     <tm id="tm2"/> 
      TEXT 2 
     <tm id="tm3"/> 
      TEXT 3 
     <tm id="tm4"/> 
      TEXT 4 
     <tm id="tm5"/> 

    </speech> 
</bml> 
<fml> 
    <some more code> 
</fml> 
</fml-apml> 

事情是我使用的DocumentBuilder庫,我可以」 t找到一種在「標籤之間插入」文本XX「的方法。有沒有辦法這樣做,或者我應該從頭開始編寫整個XML?

回答

0

您可以使用Document.createTextNode。例如

Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); 

Element speechElem = doc.createElement("speech"); 
doc.appendChild(speechElem); 

speechElem.appendChild(doc.createElement("tm")); 
speechElem.appendChild(doc.createTextNode("TEXT 1")); 
speechElem.appendChild(doc.createElement("tm")); 
speechElem.appendChild(doc.createTextNode("TEXT 2")); 
speechElem.appendChild(doc.createElement("tm")); 

導致

<speech> 
    <tm/>TEXT 1<tm/>TEXT 2<tm/> 
</speech> 
+0

釘它。謝謝 – PandaV2

0

要明確一點,你不想在任何標籤中的標籤之間包含文本?這似乎不像格式良好的XML