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 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值