2012-02-18 60 views
1

我在這裏有一個情況,我是一個java的傢伙,並得到一些困難與php。 我正在從數據庫創建一個XML文件。目前,我創建了90多個動態元素,其中一些包括屬性,子元素等,沒有任何問題。 但事情搞砸了。是否有可能取代一個dom元素的子字符串

的text1: here is a list of pencils[1]. here is a list of another type of pencils[2].

我希望有

<text1> 

here is a list of pencils <id>1</id>. here is a list of another type of pencils <id>2</id>.

</text1>

我可以代替子串([1],[2]),並插入一些其他文本,但如何用DOM元素替換這些子字符串?

任何幫助表示深深的感激..

+0

你是如何構建XML文檔的?用XMLwriter,還是純粹的字符串連接? – mhitza 2012-02-18 14:46:43

+0

嗨mhitza,謝謝你的回覆。我確實使用了xml編寫器。 – teutara 2012-02-18 14:50:38

回答

1

你不能因爲在你想要做的替換字符串是text1節點的node Value。一個變體是它的結構如下:

<text1> 
    <partial>here is a list of pencils</partial> 
    <id>1</id> 
    <partial>.here is a list of another type of pencils</partial> 
    <id>2</id> 
</text1> 

但老實說,這是不理想的。

我想什麼你得到(一秒鐘有和我)混淆是我們編寫HTML的方式:

<p>some text here <a href="...">a link</a> more <strong>variation</strong></p> 

這可能給我們的印象是,它應該是有效的XML爲好;但當然還有另外一件事要知道;該瀏覽器是否真的改變了之前的HTML格式如下(〜):

<p> 
    <textnode>some text here </textnode> 
    <a href="..."> 
    <textnode>a link</a> 
    </a> 
    <textnode> more </textnode> 
    <strong>variation</strong> 
</p> 

不是問題的答案,但我建議你重新考慮你的XML格式。

+0

謝謝mhitza。 好吧,我可能在做夢,但這裏是我的工作.. - 存儲子字符串出現次數和每個位置。 - 修剪所有子字符串 - 爲每個子字符串創建DOM元素 - 使用父元素的結束字符串。 這聽起來怎麼樣? – teutara 2012-02-18 16:34:14

相關問題