开发者

how can i add the namespace name before the attribute in xml using simplexml

开发者 https://www.devze.com 2022-12-19 16:37 出处:网络
how can 开发者_运维问答i add namespace name before a attribute of newly created tag,like <Data ss:Type=\"String\">this the value of data</Data>

how can 开发者_运维问答i add namespace name before a attribute of newly created tag,like

<Data ss:Type="String">this the value of data</Data>

but can create only

<Data Type="String">this the value of data</Data>

so i can not add "ss:" before atttbute.

Thanks in Advance

Have Dream Day


In the SimpleXml PHP manual someone gave a good example in the comments section.

Here is how I got it to work.

Say you have the following XML in a file xml.php:

<?php
$string = <<<XML
<Row>
  <Cell>
    <Data>Dolly Parton</Data>
  </Cell>
  <Cell>
    <Data>Islands in the Stream</Data>
  </Cell>
  <Cell>
    <Data>what</Data>
  </Cell>
  <Cell>
    <Data>1-29-2010</Data>
  </Cell>
</Row>
XML;

You can read in this file and add the attributes you want by using XPath.

 <?php 
        include("xml.php"); 

        $sxe = new SimpleXMLElement($string);

        $data = $sxe->xpath('//Data');

    foreach ($data as $value)
    {
        $value->addAttribute('ss:Type', 'String', 'http://www.w3.org/2001/XMLSchema-instance'); 
        }

        echo $sxe->asXML();

    ?>

This is the output I get:

<Row>
  <Cell>
    <Data xmlns:ss="http://www.w3.org/2001/XMLSchema-instance" ss:Type="String">Dolly Parton</Data>
  </Cell>
  <Cell>
    <Data xmlns:ss="http://www.w3.org/2001/XMLSchema-instance" ss:Type="String">Islands in the Stream</Data>
  </Cell>

  <Cell>
    <Data xmlns:ss="http://www.w3.org/2001/XMLSchema-instance" ss:Type="String">what</Data>
  </Cell>
  <Cell>
    <Data xmlns:ss="http://www.w3.org/2001/XMLSchema-instance" ss:Type="String">1-29-2010</Data>
  </Cell>
</Row>
0

精彩评论

暂无评论...
验证码 换一张
取 消