我正在嘗試將一些在 StringBuilder 中構建 XML 的代碼轉換為使用 dom4j。
部分代碼正在生成類似于以下結構的內容:
<foo myattribute="bar">i am some text<aninnertag>false</aninnertag>
(more text specific stuff <atag>woot</atag>)
and (another section <atag>woot again</atag> etc)
</foo>
我試圖弄清楚如何在 dom4j 中構建它。我可以為內部標簽添加元素,但它不會在有意義的背景關系中生成它。我可以將其全部添加為文本,但標簽會被轉義。
如何在 dom4j 中實作這樣的東西?甚至可能嗎?
這個 xml 很糟糕,我無法更改它。
這在輸出方面顯然是不正確的,但是一個基本的例子:
Element foo = new DefaultElement("foo");
foo.addText("i am some text" "(more text specific stuff " ")" "and (another section " " etc)");
foo.addElement("aninnertag").addText("false");
foo.addElement("atag").addText("woot");
foo.addElement("atag").addText("woot again");
uj5u.com熱心網友回復:
當您撰寫一個addText()后跟三個addElement()呼叫時,您將獲得一個 XML 內容,其中開頭有文本,結尾有 XML 元素。你必須像這樣交錯addText()和addElement()呼叫:
Element foo = new DefaultElement("foo");
foo.addAttribute("myattribute", "bar");
foo.addText("i am some text");
foo.addElement("aninnertag").addText("false");
foo.addText("(more text specific stuff ");
foo.addElement("atag").addText("woot");
foo.addText(") and (another section ");
foo.addElement("atag").addText("woot again");
foo.addText(" etc)");
System.out.println(foo.asXML());
這將生成以下輸出:
<foo myattribute="bar">i am some text<aninnertag>false</aninnertag>
(more text specific stuff <atag>woot</atag>) and (another section
<atag>woot again</atag> etc)</foo>
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/459340.html
