dongluxin2452 2014-11-12 05: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 06: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>
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部