dongshengyin0147 2013-02-10 02:29
浏览 27
已采纳

如何PHP DomDocument创建属性?

I've been smacking my head against the wall for two days trying to figure out how to get PHP to encode the XML I want. I tried SimpleXML and found out there are serious limitations, so for now I am using DomDocument to fulfill my needs. MY problem is quite basic, what is the proper syntax?

I am retrieving code from a database, then rendering it to xml. The XML structure has to be in the same exact format as the one I am going to post. The issue is when it comes to attributes. The output has three attributes that need to be repeated twelve times with different values. My problem is trying to figure out how to render the attributes, what code is necessary.

Here is the XML:

    <inits>
<version>18.05.04_EP1</version>
<source>Live</source>
<lowid>265067</lowid>
<highid>265068</highid>
<ql>300</ql>
<name>Ofab Shark Mk 1</name>
<inits slider="DEF&gt;===========][&lt;AGG" percent="100" init="430" />
<inits slider="DEF&gt;==========][=&lt;AGG" percent="90" init="530" />
<inits slider="DEF&gt;=========][==&lt;AGG" percent="81" init="630" />
<inits slider="DEF&gt;========][===&lt;AGG" percent="72" init="730" />
<inits slider="DEF&gt;=======][====&lt;AGG" percent="63" init="830" />
<inits slider="DEF&gt;======][=====&lt;AGG" percent="54" init="930" />
<inits slider="DEF&gt;=====][======&lt;AGG" percent="45" init="1030" />
<inits slider="DEF&gt;====][=======&lt;AGG" percent="36" init="1130" />
<inits slider="DEF&gt;===][========&lt;AGG" percent="27" init="1290" />
<inits slider="DEF&gt;==][=========&lt;AGG" percent="18" init="1590" />
<inits slider="DEF&gt;=][==========&lt;AGG" percent="9" init="1890" />
<inits slider="DEF&gt;][===========&lt;AGG" percent="0" init="2190" />

</inits>

Notice that Inits contains attributes, percent, and init. Which is going to display 12 times in this example, which is derived from data and php calculations. Here is the code that I am using so far. Note: I am skipping the data and calculation functions and filling in the data manually.

    $root = $doc->createElement('inits');
$root = $doc->appendChild($root);

$version = $doc->createElement('version');
$version = $root->appendChild($version);
$versiontext = $doc->createTextNode($patchNum);
$versiontext = $version->appendChild($versiontext);

$source = $doc->createElement('source');
$source = $root->appendChild($source);
$sourcetext = $doc->createTextNode('live');
$sourcetext = $source->appendChild($sourcetext);

$xlowid = $doc->createElement('lowid');
$xlowid = $root->appendChild($xlowid);
$xlowidtext = $doc->createTextNode($lowid);
$xlowidtext = $xlowid->appendChild($xlowidtext);

$xhighid = $doc->createElement('highid');
$xhighid = $root->appendChild($xhighid);
$xhighidtext = $doc->createTextNode($highid);
$xhighidtext = $xhighid->appendChild($xhighidtext);

$xql = $doc->createElement('ql');
$xql = $root->appendChild($xql);
$xqltext = $doc->createTextNode($ql);
$xqltext = $xql->appendChild($xqltext);

Where do I go from here to get the 3 attributes to work, exactly like the XML example above. Thank You.

  • 写回答

1条回答 默认 最新

  • 普通网友 2013-02-10 02:40
    关注

    To set an attribute, use $some_node->setAttribute("name","value"). Repeat as needed for all attributes.

    Also, note that you can chain function calls:

    $root = $doc->appendChild($doc->createElement('inits'));
    $root->appendChild($doc->createElement('version',$patchNum));
    $root->appendChild($doc->createElement('source',$sourcetext));
    $root->appendChild($doc->createElement('lowid',$lowid));
    $root->appendChild($doc->createElement('highid',$highid));
    $root->appendChild($doc->createElement('ql',$ql));
    for($i=11;$i>=0;$i--) {
        $node = $root->appendChild($doc->createElement('inits'));
        $node->setAttribute("slider","DEF>".str_repeat("=",$i)."][".str_repeat("=",11-$i)."<AGG");
        $node->setAttribute("percent",floor($i/11*100));
        $node->setAttribute("init",$i>3 ? 430+(11-$i)*100 : 1290+(3-$i)*300);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog