SimpleXML解析到数组

时间:2018-05-29 21:11:25

标签: php simplexml

我有这个XML片段。

<bms:ProcurementPart>
    <bms:ProcurementRefLineNum>1</bms:ProcurementRefLineNum>
    <bms:SelectedPart>
        <bms:BMSObject>
            <bms:UUID>655482ab-38ee-433f-b310-1f6f227113b9</bms:UUID>
            <bms:RefUUID>92a8ecf6-8eb6-4d1e-b88d-59b50c3b0cc9</bms:RefUUID>
        </bms:BMSObject>
        <bms:UniqueSequenceNum>1</bms:UniqueSequenceNum>
        <bms:PartNumInfo>
            <bms:PartNumType>OE</bms:PartNumType>
            <bms:PartNum>04715SNAA90ZZ</bms:PartNum>
        </bms:PartNumInfo>
        <bms:PartNumInfo>
            <bms:PartNumType>HollanderInterchange</bms:PartNumType>
            <bms:PartNum>536-01037</bms:PartNum>
        </bms:PartNumInfo>
        <bms:PartNumInfo>
            <bms:PartNumType>HollanderPType</bms:PartNumType>
            <bms:PartNum>536</bms:PartNum>
        </bms:PartNumInfo>
        <bms:PartType>PAN</bms:PartType>
        <bms:PartDesc>Caliper</bms:PartDesc>
        <bms:Quantity>1</bms:Quantity>
        <bms:PriceInfo>
            <bms:UnitListPrice>50.00</bms:UnitListPrice>
            <bms:UnitNetPrice>50.00</bms:UnitNetPrice>
        </bms:PriceInfo>
    </bms:SelectedPart>
    <bms:NeedByDateTime>0001-01-01T00:00:00</bms:NeedByDateTime>
</bms:ProcurementPart>
<bms:ProcurementPart>
    <bms:ProcurementRefLineNum>2</bms:ProcurementRefLineNum>
    <bms:SelectedPart>
        <bms:BMSObject>
            <bms:UUID>eb510b2d-c8e2-4dd1-a214-a017c22b3104</bms:UUID>
            <bms:RefUUID>92a8ecf6-8eb6-4d1e-b88d-59b50c3b0cc9</bms:RefUUID>
        </bms:BMSObject>
        <bms:UniqueSequenceNum>3</bms:UniqueSequenceNum>
        <bms:PartNumInfo>
            <bms:PartNumType>OE</bms:PartNumType>
            <bms:PartNum>71570SNAA00</bms:PartNum>
        </bms:PartNumInfo>
        <bms:PartNumInfo>
            <bms:PartNumType>HollanderInterchange</bms:PartNumType>
            <bms:PartNum>536-01036</bms:PartNum>
        </bms:PartNumInfo>
        <bms:PartType>PAN</bms:PartType>
        <bms:PartDesc>Caliper</bms:PartDesc>
        <bms:Quantity>1</bms:Quantity>
        <bms:PriceInfo>
            <bms:UnitListPrice>30.16</bms:UnitListPrice>
            <bms:UnitNetPrice>30.16</bms:UnitNetPrice>
        </bms:PriceInfo>
    </bms:SelectedPart>
    <bms:NeedByDateTime>0001-01-01T00:00:00</bms:NeedByDateTime>
</bms:ProcurementPart>

使用此foreach进行迭代:

foreach($ProcurementPartNodes as $node) {
    $uniqueseq++;
    $linenum  = (string)$node->children("bms",true)->ProcurementRefLineNum;
    $resp["ProcurementRefLineNum"] = $linenum;
    $selected = $BuildSelectedPart($node, $linenum, $uniqueseq);
    $resp["SelectedPart"] = $selected;
    $resp["NeedByDateTime"] = "0001-01-01T00:00:00";
    $response_data["ProcurementPart"][] = $resp;
}

每个人都尝试使用这些函数获取每个SelectedNode信息:

function BuildSelectedPart($node, $procurementInfoUUID, $linenum, $uniqueseq) {
    $partdesc      = (string)$node[0]->children("bms", true)->PartDesc;
    $qty           = (int)$node[0]->children("bms", true)->Quantity;
    $unitlistprice = (string)$node[0]->children("bms",true)->PriceInfo->UnitListPrice;
    $unitnetprice  = (string)$node[0]->children("bms",true)->PriceInfo->UnitNetPrice;
    unset($selected);
    $selected["BMSObject"] = $BuildSelectedBMS($procurementInfoUUID);
    $selected["UniqueSequenceNum"] = $uniqueseq;      // Iterator for SelectedPart count
    // Iterate over the PartNumInfo nodes
    $partnumInfoNodes = $node[0]->xpath("//*[bms:ProcurementRefLineNum = ".$linenum."]/bms:SelectedPart/bms:PartNumInfo");
    foreach($partnumInfoNodes as $pnodes) {
        echo $pnodes->asXML();
        foreach($pnodes as $p) {
            $pnum = array(
                "PartNumType"=>(string)$p->children("bms",true)->PartNumInfo->PartNumType,// Can be OE, HollanderInterchange, or Stock
                "PartNum"=>(string)$p->children("bms",true)->PartNumInfo->PartNum         // Corresponding number of above type
            ); // PartNumInfo
        }
        $selected["PartNumInfo"][] = $pnum;
    }
    $selected["PartType"] = "PAL";                    // Usually PAL
    $selected["PartDesc"] = $partdesc;                // Description of Part Requested
    $selected["Quantity"] = $qty;                     // Quantity Requested
    $selected["PriceInfo"] = array(
            "UnitListPrice"=>$unitlistprice,          // Price Requested
            "UnitNetPrice"=>$unitnetprice             // Price Requested
        ); // PriceInfo                        
    return $selected;        
} // function BuildSelectedPart()

function BuildSelectedBMS($procurementInfoUUID) {
    $UUID = $this->CreateGuid("",true); // creates a new Guid value
    return array(
            "UUID"=>$UUID,                            // Generated UUID
            "RefUUID"=>$procurementInfoUUID           // ProcurementInfo.BMSObject UUID value from above
       ); // BMSObject        
}

这是预期的,我似乎没有正确地从PartNum和PriceInfo部分获取值。一直在打我的脑袋 这个约一个小时了......

[ProcurementPart] => Array(
        [0] => Array(
                [ProcurementRefLineNum] => 1
                [SelectedPart] => Array(
                        [BMSObject] => Array(
                                [UUID] => 860a7915-8772-23fa-da40-5ca17ada8d50
                                [RefUUID] => eb9dffc6-de19-e0bf-536e-dc1e4a15344a
                        )
                        [UniqueSequenceNum] => 1
                        [PartNumInfo] => Array(
                                [0] => Array(
                                        [PartNumType] => OE
                                        [PartNum] => 04715SNAA90ZZ
                                )
                                [1] => Array(
                                        [PartNumType] => HollanderInterchange
                                        [PartNum] => 536-01037
                                )
                                [2] => Array(
                                        [PartNumType] => HollanderPType
                                        [PartNum] => 536
                                )
                        )
                        [PartType] => PAL
                        [PartDesc] => 
                        [Quantity] => 0
                        [PriceInfo] => Array(
                                [UnitListPrice] => 50.00
                                [UnitNetPrice] => 50.00
                        )
                )
                [NeedByDateTime] => 0001-01-01T00:00:00
        )
        [1] => Array(
                [ProcurementRefLineNum] => 2
                [SelectedPart] => Array(
                        [BMSObject] => Array(
                                [UUID] => eeb17d4e-b1bb-b92c-b8ac-b311a21389e6
                                [RefUUID] => eb9dffc6-de19-e0bf-536e-dc1e4a15344a
                         )
                        [UniqueSequenceNum] => 2
                        [PartNumInfo] => Array(
                                [0] => Array(
                                        [PartNumType] => OE
                                        [PartNum] => 71570SNAA00
                                )
                                [1] => Array(
                                        [PartNumType] => HollanderInterchange
                                        [PartNum] => 536-01036
                                )
                        )
                        [PartType] => PAL
                        [PartDesc] => 
                        [Quantity] => 0
                        [PriceInfo] => Array(
                                [UnitListPrice] => 30.16
                                [UnitNetPrice] => 30.16
                        )
                    )
                [NeedByDateTime] => 0001-01-01T00:00:00
            )
    )

这就是我得到的:

[ProcurementPart] => Array(
        [0] => Array(
                [ProcurementRefLineNum] => 1
                [SelectedPart] => Array(
                        [BMSObject] => Array(
                                [UUID] => 860a7915-8772-23fa-da40-5ca17ada8d50
                                [RefUUID] => eb9dffc6-de19-e0bf-536e-dc1e4a15344a
                        ),
                        [UniqueSequenceNum] => 1
                        [PartNumInfo] => Array(
                                [0] => Array(
                                        [PartNumType] => 
                                        [PartNum] => 
                                ),
                                [1] => Array(
                                        [PartNumType] => 
                                        [PartNum] => 
                                ),
                                [2] => Array(
                                        [PartNumType] => 
                                        [PartNum] => 
                                )
                        )
                        [PartType] => PAL
                        [PartDesc] => 
                        [Quantity] => 0
                        [PriceInfo] => Array(
                                [UnitListPrice] => 
                                [UnitNetPrice] => 
                        )
                )
                [NeedByDateTime] => 0001-01-01T00:00:00
        )
        [1] => Array(
                [ProcurementRefLineNum] => 2
                [SelectedPart] => Array(
                        [BMSObject] => Array(
                                [UUID] => eeb17d4e-b1bb-b92c-b8ac-b311a21389e6
                                [RefUUID] => eb9dffc6-de19-e0bf-536e-dc1e4a15344a
                         ),
                        [UniqueSequenceNum] => 2
                        [PartNumInfo] => Array(
                                [0] => Array(
                                        [PartNumType] => 
                                        [PartNum] => 
                                ),
                                [1] => Array(
                                        [PartNumType] => 
                                        [PartNum] => 
                                )
                        )
                        [PartType] => PAL
                        [PartDesc] => 
                        [Quantity] => 0
                        [PriceInfo] => Array(
                                [UnitListPrice] => 
                                [UnitNetPrice] => 
                        )
                    )
                [NeedByDateTime] => 0001-01-01T00:00:00
            )
    )

1 个答案:

答案 0 :(得分:1)

对于您的部件号数据,请将该位更改为...

foreach($partnumInfoNodes as $pnodes) {
    $pnum = array(
        "PartNumType"=>(string)$pnodes->children("bms",true)->PartNumType,// Can be OE, HollanderInterchange, or Stock
        "PartNum"=>(string)$pnodes->children("bms",true)->PartNum         // Corresponding number of above type
    ); // PartNumInfo
    $selected["PartNumInfo"][] = $pnum;
}

你试图进入另一个级别而你并不需要。

我还认为您的第一组数据需要额外的级别<SelectedPart> ...

$partdesc      = (string)$node[0]->children("bms", true)->SelectedPart->PartDesc;
$qty           = (int)$node[0]->children("bms", true)->SelectedPart->Quantity;
$unitlistprice = (string)$node[0]->children("bms",true)->SelectedPart->PriceInfo->UnitListPrice;
$unitnetprice  = (string)$node[0]->children("bms",true)->SelectedPart->PriceInfo->UnitNetPrice;