2012-02-18 53 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

相关问题