载入非标准xml WSMAN数据转换成用PHP对象(Loading non-standard xml

2019-10-21 03:14发布

这个问题已经在许多变化已经回答了,但他们没有提到我的情况。

我使用的WSMan,然后返回输出作为一种须藤-XML的拉动数据。 我甚至不认为这是“真正”的XML,因为它有许多不规范的属性。 的问题是,我需要能够引用输出作为内PHP的对象。 所以目前我使用了很多str_replace函数的。 这样做的问题是,如果非标准格式偏离(在某些情况下,它会返回这样的事情<KeyID xsi:nil="true"/>在其他情况下,它可能是这样的<CMCIP xsi:nil="true"/>它是很难预见所有的不同属性,我将不得不考虑和导入它作为使用simplexml_load_string一个对象之前拔出可变的。

所以,我在所有简单的问题:有没有办法来加载非标准XML到对象? 这里是XML数据的样本,让你知道我们这里处理什么疯狂。

<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsen="http://schemas.xmlsoap.org/ws/2004/09/enumeration">
  <s:Header>
    <wsa:To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:To>
    <wsa:Action>http://schemas.xmlsoap.org/ws/2004/09/enumeration/EnumerateResponse</wsa:Action>
    <wsa:RelatesTo>uuid:3ae2d181-04f0-14f0-8002-89040b5d1500</wsa:RelatesTo>
    <wsa:MessageID>uuid:43a291ab-04f0-14f0-8073-b516f1d9bed4</wsa:MessageID>
  </s:Header>
  <s:Body>
    <wsen:EnumerateResponse>
      <wsen:EnumerationContext>439c90e9-04f0-14f0-8072-b516f1d9bed4</wsen:EnumerationContext>
    </wsen:EnumerateResponse>
  </s:Body>
</s:Envelope>
<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsen="http://schemas.xmlsoap.org/ws/2004/09/enumeration" xmlns:n1="http://schemas.dell.com/wbem/wscim/1/cim-schema/2/DCIM_SystemView" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <s:Header>
    <wsa:To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:To>
    <wsa:Action>http://schemas.xmlsoap.org/ws/2004/09/enumeration/PullResponse</wsa:Action>
    <wsa:RelatesTo>uuid:3af0a1eb-04f0-14f0-8003-89040b5d1500</wsa:RelatesTo>
    <wsa:MessageID>uuid:43a41fe8-04f0-14f0-8074-b516f1d9bed4</wsa:MessageID>
  </s:Header>
  <s:Body>
    <wsen:PullResponse>
      <wsen:Items>
        <n1:DCIM_SystemView>
          <n1:AssetTag/>
          <n1:BIOSReleaseDate>11/20/2013</n1:BIOSReleaseDate>
          <n1:BIOSVersionString>2.1.3</n1:BIOSVersionString>
          <n1:BaseBoardChassisSlot>NA</n1:BaseBoardChassisSlot>
          <n1:BatteryRollupStatus>1</n1:BatteryRollupStatus>
          <n1:BladeGeometry>255</n1:BladeGeometry>
          <n1:BoardPartNumber>061P35A00</n1:BoardPartNumber>
          <n1:BoardSerialNumber>CN70163231007K</n1:BoardSerialNumber>
          <n1:CMCIP xsi:nil="true"/>
          <n1:CPLDVersion>1.0.3</n1:CPLDVersion>
          <n1:CPURollupStatus>1</n1:CPURollupStatus>
          <n1:ChassisModel/>
          <n1:ChassisName>Main System Chassis</n1:ChassisName>
          <n1:ChassisServiceTag>REMOVED</n1:ChassisServiceTag>
          <n1:ChassisSystemHeight>2</n1:ChassisSystemHeight>
          <n1:DeviceDescription>System</n1:DeviceDescription>
          <n1:ExpressServiceCode>33088672189</n1:ExpressServiceCode>
          <n1:FQDD>System.Embedded.1</n1:FQDD>
          <n1:FanRollupStatus>1</n1:FanRollupStatus>
          <n1:HostName/>
          <n1:InstanceID>System.Embedded.1</n1:InstanceID>
          <n1:LastSystemInventoryTime>20140928010936.000000+000</n1:LastSystemInventoryTime>
          <n1:LastUpdateTime>20140220171215.000000+000</n1:LastUpdateTime>
          <n1:LicensingRollupStatus>1</n1:LicensingRollupStatus>
          <n1:LifecycleControllerVersion>2.1.0</n1:LifecycleControllerVersion>
          <n1:Manufacturer>Dell Inc.</n1:Manufacturer>
          <n1:MaxCPUSockets>2</n1:MaxCPUSockets>
          <n1:MaxDIMMSlots>24</n1:MaxDIMMSlots>
          <n1:MaxPCIeSlots>6</n1:MaxPCIeSlots>
          <n1:MemoryOperationMode>OptimizerMode</n1:MemoryOperationMode>
          <n1:Model>PowerEdge R720xd</n1:Model>
          <n1:NodeID>F7852V1</n1:NodeID>
          <n1:PSRollupStatus>1</n1:PSRollupStatus>
          <n1:PlatformGUID>3156324f-c0c6-3580-3810-00374c4c4544</n1:PlatformGUID>
          <n1:PopulatedCPUSockets>2</n1:PopulatedCPUSockets>
          <n1:PopulatedDIMMSlots>8</n1:PopulatedDIMMSlots>
          <n1:PopulatedPCIeSlots>2</n1:PopulatedPCIeSlots>
          <n1:PowerCap>598</n1:PowerCap>
          <n1:PowerCapEnabledState>3</n1:PowerCapEnabledState>
          <n1:PowerState>2</n1:PowerState>
          <n1:PrimaryStatus>1</n1:PrimaryStatus>
          <n1:RollupStatus>1</n1:RollupStatus>
          <n1:ServerAllocation xsi:nil="true"/>
          <n1:ServiceTag>REMOVED</n1:ServiceTag>
          <n1:StorageRollupStatus>1</n1:StorageRollupStatus>
          <n1:SysMemErrorMethodology>6</n1:SysMemErrorMethodology>
          <n1:SysMemFailOverState>NotInUse</n1:SysMemFailOverState>
          <n1:SysMemLocation>3</n1:SysMemLocation>
          <n1:SysMemMaxCapacitySize>1572864</n1:SysMemMaxCapacitySize>
          <n1:SysMemPrimaryStatus>1</n1:SysMemPrimaryStatus>
          <n1:SysMemTotalSize>65536</n1:SysMemTotalSize>
          <n1:SystemGeneration>12G Monolithic</n1:SystemGeneration>
          <n1:SystemID>1320</n1:SystemID>
          <n1:SystemRevision>0</n1:SystemRevision>
          <n1:TempRollupStatus>1</n1:TempRollupStatus>
          <n1:UUID>4c4c4544-0037-3810-8035-c6c04f325631</n1:UUID>
          <n1:VoltRollupStatus>1</n1:VoltRollupStatus>
          <n1:smbiosGUID>44454c4c-3700-1038-8035-c6c04f325631</n1:smbiosGUID>
        </n1:DCIM_SystemView>
      </wsen:Items>
      <wsen:EndOfSequence/>
    </wsen:PullResponse>
  </s:Body>
</s:Envelope>

Answer 1:

你获得了作为响应/输出多个XML文档的串联。 在您的例子这是两个。

这不是有效的XML,但它也并不少见。

因此,所有你需要做的是分裂的文件,并选择你需要要处理(在你的例子第二)之一:

$split = preg_split('~\Q<?xml version="1.0" encoding="UTF-8"?>\E\R~u', $sequenced_xml, 2, PREG_SPLIT_NO_EMPTY);
$xml   = simplexml_load_string($split[1]);

正如你现在有XML文档your're兴趣,你可以做什么都很多其他的答案如何解析SOAP响应建议。 有没有畸形XML(实际上是串联简洁(wellformed)XML文档的顺序)的任何更长的时间。

其余的正在处理的命名空间。

一些指针:

...得到SOAP信封体:

$soap = 'http://www.w3.org/2003/05/soap-envelope';
$body = $xml->children($soap)->Body;

......所有的列举项目为数组:

$wsen = 'http://schemas.xmlsoap.org/ws/2004/09/enumeration';
$xml->registerXPathNamespace('wsen', $wsen);
$items = $body->xpath('.//wsen:*/*[not(namespace-uri(.) = namespace-uri(..))]');

等等等等。

在你自己的代码,例如一个音符你给你的答案是:如果你在任何命名空间中寻找一个元素的名称,你可以做到这一点在XPath的local-name()

$pullitem = 'ServiceTag';
$try      = $xml->xpath(sprintf("//*[local-name(.)='%s']", $pullitem));

printf("pullitem '%s' has been foun in the following namespaces:\n", $pullitem);
foreach ($try as $element) {
    $nsURI = dom_import_simplexml($element)->namespaceURI;
    printf(" - %s\n", $nsURI);
}

该作备用您在五年左右的个人XPath的电话。

如果你终于不希望因为你希望它是每个项目的“那一个”反正关心的命名空间,你可以用DOM文档的帮助下各提取到一个新的文件是为每个结果一个SimpleXMLElement拥有:

/**
 * create a new SimpleXMLElement out of an existing one
 *
 * @param SimpleXMLElement $item
 *
 * @return SimpleXMLElement
 */
function simplexml_export_element(SimpleXMLElement $item) {
    $doc  = new DOMDocument();
    $node = $doc->importNode(dom_import_simplexml($item), true);
    $node = $doc->appendChild($node);
    return simplexml_load_string($doc->saveXML($doc->documentElement), get_class($item), 0, $node->namespaceURI);
}

这种辅助程序是有帮助的,因为它把元素的自己的名字空间的命名空间,新的SimpleXMLElement。 这允许直接访问的孩子在同一个命名空间。 进一步使用它的代码将不再需要关心这个“默认”的命名空间明确。

例:

$split = preg_split('~\Q<?xml version="1.0" encoding="UTF-8"?>\E\R~u', $sequenced_xml, 2, PREG_SPLIT_NO_EMPTY);

$xml  = new SimpleXMLElement($split[1]);

$soap = 'http://www.w3.org/2003/05/soap-envelope';
$body = $xml->children($soap)->Body;

$wsen = 'http://schemas.xmlsoap.org/ws/2004/09/enumeration';
$xml->registerXPathNamespace('wsen', $wsen);

$enumerated = $body->xpath('.//wsen:*/*[not(namespace-uri(.) = namespace-uri(..))]');
$enumerated = array_map('simplexml_export_element', $enumerated);

foreach ($enumerated as $item) {
    echo $item->getName(), "\n";
    foreach ($item as $key => $value) {
        printf(" - %s: %s\n", $key, $value);
    }
}

输出:

DCIM_SystemView
 - AssetTag: 
 - BIOSReleaseDate: 11/20/2013
 - BIOSVersionString: 2.1.3
 - BaseBoardChassisSlot: NA
 - BatteryRollupStatus: 1
 - BladeGeometry: 255
 - BoardPartNumber: 061P35A00
 - BoardSerialNumber: CN70163231007K
 - CMCIP: 
 - CPLDVersion: 1.0.3
 - CPURollupStatus: 1
 - ChassisModel: 
 - ChassisName: Main System Chassis
 - ChassisServiceTag: REMOVED
 - ChassisSystemHeight: 2
 - DeviceDescription: System
 - ExpressServiceCode: 33088672189
 - FQDD: System.Embedded.1
 - FanRollupStatus: 1
 - HostName: 
 - InstanceID: System.Embedded.1
 - LastSystemInventoryTime: 20140928010936.000000+000
 - LastUpdateTime: 20140220171215.000000+000
 - LicensingRollupStatus: 1
 - LifecycleControllerVersion: 2.1.0
 - Manufacturer: Dell Inc.
 - MaxCPUSockets: 2
 - MaxDIMMSlots: 24
 - MaxPCIeSlots: 6
 - MemoryOperationMode: OptimizerMode
 - Model: PowerEdge R720xd
 - NodeID: F7852V1
 - PSRollupStatus: 1
 - PlatformGUID: 3156324f-c0c6-3580-3810-00374c4c4544
 - PopulatedCPUSockets: 2
 - PopulatedDIMMSlots: 8
 - PopulatedPCIeSlots: 2
 - PowerCap: 598
 - PowerCapEnabledState: 3
 - PowerState: 2
 - PrimaryStatus: 1
 - RollupStatus: 1
 - ServerAllocation: 
 - ServiceTag: REMOVED
 - StorageRollupStatus: 1
 - SysMemErrorMethodology: 6
 - SysMemFailOverState: NotInUse
 - SysMemLocation: 3
 - SysMemMaxCapacitySize: 1572864
 - SysMemPrimaryStatus: 1
 - SysMemTotalSize: 65536
 - SystemGeneration: 12G Monolithic
 - SystemID: 1320
 - SystemRevision: 0
 - TempRollupStatus: 1
 - UUID: 4c4c4544-0037-3810-8035-c6c04f325631
 - VoltRollupStatus: 1
 - smbiosGUID: 44454c4c-3700-1038-8035-c6c04f325631

希望这仍然是你的情况有所帮助。



Answer 2:

谢谢你@保罗Crovella的评论,这其实让我在正确的轨道上。 最后我学到了很多东西。 在任何情况下,解决方案最终被一个事物的组合。 这个职位是最帮助,但是:

如何阅读由PHP SOAP信封回复

对于那些好奇我怎么得到它的工作,我最后写,它将分析这个为我的功能。 为了您的舒适的房子位于下方。 希望这可以帮助其他人下来谁找我同样的事情在路上!

function pull_wsman_idrac($run_iDRAC_IP,$user,$pass,$namespace,$pullitem,$context="NULL"){
    //Example pull_wsman("10.10.10.10","root","calvin","DCIM_SystemView","ServiceTag")
    //Possible namespaces are listed on the DCIM profile page on Dell's website. http://en.community.dell.com/techcenter/systems-management/w/wiki/1906.dcim-library-profile
    //Namspaces used (primary) : DCIM_SystemView, DCIM_CPUView, DCIM_ControllerView, DCIM_VFlashView, DCIM_MemoryView, DCIM_PCIDeviceView, DCIM_NICView, DCIM_IDRACCARDView
    $wsman_output = shell_exec("wsman enumerate http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/root/dcim/$namespace -h $run_iDRAC_IP -V -v -c dummy.cert -P 443 -u $user -p $pass -j utf-8 -y basic");
    $wsman_output = preg_replace('/\<\?xml version="1.0" encoding="UTF-8"\?\>/', '', $wsman_output);
    $wsman_output = preg_replace('/\<\?xml version="1.0" encoding="utf-8"\?\>/', '', $wsman_output);
    $wsman_output = trim($wsman_output);
    $conv = <<<XML  
    <root> $wsman_output </root> 
    XML;
    $xml = new SimpleXMLElement($conv);
    $xml->registerXPathNamespace("s", "http://www.w3.org/2003/05/soap-envelope");
    $xml->registerXPathNamespace("wsa", "http://schemas.xmlsoap.org/ws/2004/08/addressing");
    $xml->registerXPathNamespace("wsen", "http://schemas.xmlsoap.org/ws/2004/09/enumeration");
    $xml->registerXPathNamespace("n1", "http://schemas.dell.com/wbem/wscim/1/cim-schema/2/$namespace");
    $xml->registerXPathNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
    if($context == "NULL"){
        $try = $xml->xpath("//s:$pullitem");
        if(!empty($try)){ $cpath="s"; goto gotit;   }
        $try = $xml->xpath("//wsa:$pullitem");
        if(!empty($try)){ $cpath="wsa"; goto gotit; }
        $try = $xml->xpath("//wsen:$pullitem");
        if(!empty($try)){ $cpath="wsen"; goto gotit;    }
        $try = $xml->xpath("//n1:$pullitem");
        if(!empty($try)){ $cpath="n1"; goto gotit;  }
        $try = $xml->xpath("//xsi:$pullitem");
        if(!empty($try)){ $cpath="xsi"; goto gotit; }
        gotit:
        if(empty($cpath)){ return 0; }
            else{
                $a = array();
                $i = 0;
                foreach($try as $tried){
                    foreach($tried->xpath("//n1:*") as $trys) { 
                    $go = $trys->getName();
                    //$i++; echo "$i : " . $go . "\n";
                    $a[] = $go;
                }
            }
            $a = array_unique($a);
            return $a;      
        }
    }
    elseif($context !== "NULL"){
        $try = $xml->xpath("//$context:$pullitem");
        $a = array();
        foreach($try as $extract){
            $a[] = $extract;
        }
        if(!empty($a)){
            return $a;
        }
        else{ return 0; }
    }
}


文章来源: Loading non-standard xml wsman data into object with php