2013-03-25 27 views
-1

我有一個已經制作好的xml表單,我花了幾個小時在網上尋找將屬性添加到某些元素的方法。任何幫助將是偉大的!如果您需要更多信息,例如XML的某些部分,請告訴我,我會很樂意提供您需要的東西!如何使用PHP將屬性添加到已經創建的元素?

+0

[你嘗試過什麼?](http://www.whathaveyoutried.com/)請參閱[ask advice](http://stackoverflow.com/questions/ask-advice)。 – 2013-03-25 00:48:03

+0

我嘗試過使用DOM元素以及simpleXML。 simpleXML非常好用,但問題在於它將XML全部保存在一行中,這是我無法做到的,因爲它需要人性化。 DOM對我的理解非常複雜。 – 2013-03-25 00:51:52

+0

發佈你的xml片段 – michi 2013-03-25 01:05:53

回答

0

使用simplexml的作業和DOM來很好地輸出它:

$xmlstr = <<<XML 
<root> 
    <fruit origin="Brazil">Banana</fruit> 
    <fruit origin="Germany">Apple</fruit> 
    <fruit origin="Spain">Tomato</fruit> 
</root> 
XML; 

$xml = simplexml_load_string($xmlstr); 

$xml->fruit[0]->addAttribute("state","fresh"); // add state="fresh" to Banana 

看到如何生成的傳球$ XML結構化輸出到DOM這個帖子: PHP simpleXML how to save the file in a formatted way?

相關問題