gpt4 book ai didi

php - 如何在 XML 中创建和设置属性值?

转载 作者:可可西里 更新时间:2023-11-01 13:48:20 25 4
gpt4 key购买 nike

如何在 XML 中为属性创建和设置值?我需要设置 ateam id="101"吗?有了这个,我列出了我的代码。通过使用我创建的 createAttribute 但我不知道如何为该属性设置值..

/* create XML Content */
$domtree = new DOMDocument('1.0', 'UTF-8');
$xmlRoot = $domtree->createElement("xml");
$xmlRoot = $domtree->appendChild($xmlRoot);
$currentTrack = $domtree->createElement("messsage");
$currentTrack = $xmlRoot->appendChild($currentTrack);
$currentTrack->appendChild($domtree->createElement('category','Scores'));
$Game = $currentTrack->appendChild($domtree->createElement('Game',''));
$Game->appendChild($v = $domtree->createElement('ateam','India'));
$Game->appendChild($domtree->createElement('score',30));
$v->appendChild($domtree->createAttribute('id'));
echo $domtree->saveXML();

输出

<?xml version="1.0" encoding="UTF-8"?>
<xml>
<messsage>
<category>Scores</category>
<Game>
<ateam id="">India</ateam>
<score>30</score>
</Game>
</messsage>
</xml>

预期输出

 <?xml version="1.0" encoding="UTF-8"?>
<xml>
<messsage>
<category>Scores</category>
<Game>
<ateam id="101">India</ateam>
<score>30</score>
</Game>
</messsage>
</xml>

最佳答案

最简单的方法是使用 setAttribute :

$v->setAttribute('id', 101);

关于php - 如何在 XML 中创建和设置属性值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6226702/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com