dongluxin2452 2014-11-12 13:24
浏览 60
已采纳

使用PHP设置XML标记的名称空间

I'd like to create an XML document with a very specific format. It should look similar to this:

<?xml version="1.0" encoding="UTF-8"?>
<ram:FLOW xmlns:ram=\"http://MY_LIBRARY\" xmlns:mar=\"http://ANOTHER_LIBRARY\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">
<Header>
<Source>Application1</Source>
<Time>2014-11-12T12:46:39</Time>
<Environment>TEST</Environment>
<Sequence>537</Sequence>
</Header>
<Data>
<mar:OC_DC>
<DC_elements>
<Unit>
<Unit_ID>089789</Unit_ID>
<State>active</State>
</Unit>
<Unit>
<Unit_ID>459008</Unit_ID>
<State>inactive</State>
</Unit>
</DC_elements>
</mar:OC_DC>
</Data>
</ram:FLOW>

I wrote a PHP/MySQL script to generate this document:

<?php   
$xml = new SimpleXMLElement("<?xml version=\"1.0\" encoding=\"UTF-8\"?><ram:FLOW xmlns:ram=\"http://MY_LIBRARY\" xmlns:mar=\"http://ANOTHER_LIBRARY\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"></ram:FLOW>");

$header = $xml->addChild('Header');
$header->addChild('Source', $source);
$header->addChild('Time', $time);
$header->addChild('Environment', $env);
$header->addChild('Sequence', $sequence);

$data=$xml->addChild('Data');
$mar_oc_dc=$data->addChild('mar:OC_DC');
$dc_elements=$mar_oc_dc->addChild('DC_elements');

while($condition)
{
   // some MySQL code here to extract unit_id and state
   $unit=$dc_elements->addChild('Unit');
   $unit_id=$unit->addChild('Unit_ID', $unit_id);
   $state=$unit->addChild('State', $state);
}

$dom = new DOMDocument();
$dom->preserveWhiteSpace = FALSE;
$dom->formatOutput = TRUE;
$dom->loadXML($xml->asXML());
$handle = fopen("backup/" . $file_name . ".xml", "w");
fwrite($handle, $dom->saveXML());
fclose($handle);            
?>

But the result was a little bit different from what I expected:

<?xml version="1.0" encoding="UTF-8"?>
<FLOW xmlns:ram=\"http://MY_LIBRARY\" xmlns:mar=\"http://ANOTHER_LIBRARY\"     xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">
<Header>
<Source>Application1</Source>
<Time>2014-11-12T12:46:39</Time>
<Environment>TEST</Environment>
<Sequence>537</Sequence>
</Header>
<Data>
<OC_DC>
<DC_elements>
<Unit>
<Unit_ID>089789</Unit_ID>
<State>active</State>
</Unit>
<Unit>
<Unit_ID>459008</Unit_ID>
<State>inactive</State>
</Unit>
</DC_elements>
</OC_DC>
</Data>
</FLOW>

As you can see, the ram:FLOW tag became FLOW, and the mar:OC_DC tag became OC_DC. I looked on Stack Overflow and other websites for a solution and didn't manage to find one. Could you please give me a hand with this?

Thank you in advance.

  • 写回答

1条回答 默认 最新

  • duanmu2013 2014-11-12 14:21
    关注

    The xmlns:* attributes are namespace definitions (not libraries). The value of that attributes is a unique string that identifies the format/standard the elements belong to.

    The attributes define a prefix for the unique string so that the XML document is smaller and more readable.

    If you want to create an element (or attribute) inside a namespace you have to provide the namespace. In SimpleXMlElement the third argument is the namespace.

    It seems to add the elements to the namespace of the parent node, if no namespace is provided. That means that you have to provide an empty string for any element without a namespace.

    $root = new SimpleXMlElement('<ram:FLOW xmlns:ram="http://MY_LIBRARY" xmlns:mar="http://ANOTHER_LIBRARY"/>');
    $root->addChild('header', null, '');
    $data = $root->addChild('data', null, '');
    $data->addChild('mar:OC_DC', null, 'http://ANOTHER_LIBRARY');
    
    echo $root->asXml();
    

    Output:

    <?xml version="1.0"?>
    <ram:FLOW xmlns:ram="http://MY_LIBRARY" xmlns:mar="http://ANOTHER_LIBRARY">
       <header xmlns=""/>
       <data xmlns="">
           <mar:OC_DC/>
       </data>
    </ram:FLOW>
    

    I haven't found a way to avoid the empty xmlns attributes.

    DOM is more explicit. The create and append logic is separate.

    const XMLNS_RAM = 'http://MY_LIBRARY';
    const XMLNS_MAR = 'http://ANOTHER_LIBRARY';
    
    $dom = new DOMDocument();
    // appending an element with a namespace with define it if needed
    $root = $dom->appendChild($dom->createElementNS(XMLNS_RAM, 'ram:FLOW'));
    // setting the xmlns attribute explicit avoids the definition in descendant nodes
    $root->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:mar', XMLNS_MAR);
    $root->appendChild($dom->createElement('header'));
    $data = $root->appendChild($dom->createElement('data'));
    $data->appendChild($dom->createElementNS(XMLNS_MAR, 'mar:OC_DC'));
    
    $dom->formatOutput = true;
    echo $dom->saveXml();
    

    Output:

    <?xml version="1.0"?>
    <ram:FLOW xmlns:ram="http://MY_LIBRARY" xmlns:mar="http://ANOTHER_LIBRARY">
      <header/>
      <data>
        <mar:OC_DC/>
      </data>
    </ram:FLOW>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥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