【问题标题】:XML Changing value of namespace propertyXML 更改命名空间属性的值
【发布时间】:2016-07-01 18:08:07
【问题描述】:

在将 XML 文档提交给 EPP 服务之前,我需要更改嵌套命名空间属性的值。

 <?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 中,我需要更改 host:name 元素的值。我正在使用 PHP simplexml_load_string 首先修改 XML 模式中的值,如下所示。

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

这样做的正确方法是什么?

【问题讨论】:

    标签: php xml


    【解决方案1】:

    使用以下代码

    请注意,我在附上数据供您参考如何使用它

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

    我尝试过的全部简单代码

    $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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多