doutang7707 2016-07-01 18:08
浏览 41

XML更改名称空间属性的值

I need to change a value of a nested namespace property within a XML document before I submit it to an EPP service.

 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
 <epp xmlns="urn:ietf:params:xml:ns:epp-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      xsi:schemaLocation="urn:ietf:params:xml:ns:epp-1.0 epp-1.0.xsd">
   <command>
     <info>
       <host:info xmlns:host="urn:ietf:params:xml:ns:host-1.0" xsi:schemaLocation="urn:ietf:params:xml:ns:host-1.0 host-1.0.xsd">
         <host:name>ns1.example.test.example.com</host:name>
       </host:info>
     </info>
     <clTRID>NORID-14373-1207137695427775</clTRID>
   </command>
 </epp>

In the above XML, I need to change the host:name elements value. I am using PHP simplexml_load_string to first modify the values from the XML schema as shown below.

 $xml = simplexml_load_string(file_get_contents($fn));
 $xml->command->clTRID = GUID(); // This works perfectly
 $xml->command->info->name = 'somename'; // Does not work :)

What is the correct way to do this?

  • 写回答

1条回答 默认 最新

  • dongshi4773 2016-07-01 18:20
    关注

    Use the following code

    Note I am echoing the data for your reference as how to use it

    echo $xml->command->info->children('host', true)->info->name;
    

    Total simple code which I tried

    $xmls = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
     <epp xmlns="urn:ietf:params:xml:ns:epp-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      xsi:schemaLocation="urn:ietf:params:xml:ns:epp-1.0 epp-1.0.xsd">
       <command>
         <info>
           <host:info xmlns:host="urn:ietf:params:xml:ns:host-1.0" xsi:schemaLocation="urn:ietf:params:xml:ns:host-1.0 host-1.0.xsd">
             <host:name>ns1.example.test.example.com</host:name>
           </host:info>
         </info>
         <clTRID>NORID-14373-1207137695427775</clTRID>
       </command>
     </epp>';
    
     $xml = simplexml_load_string($xmls);
     $xml->command->clTRID = 'something'; // This works perfectly
     echo $xml->command->info->children('host', true)->info->name; 
    
     $xml->command->info->children('host', true)->info->name = 'some new name';
     echo $xml->command->info->children('host', true)->info->name;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测