如何在php的父节点中添加xml

时间:2018-12-03 10:23:18

标签: php xml

我有2个xml字符串,来自以下2个函数:

1)$ accessRequestXML = $ accessRequesttXML-> asXML();

它产生以下xml字符串:

<?xml version="1.0"?>
<AccessRequest>
    <AccessLicenseNumber>7D54A69331203795</AccessLicenseNumber>
    <UserId>peterfelts</UserId>
    <Password>abc123</Password>
<AccessRequest>

2)     $ rateRequestXml = $ rateRequestXML-> asXML();

它产生以下xml字符串:

<?xml version="1.0"?>
<RatingServiceSelectionRequest>
    <Request>
        <RequestAction>Rate</RequestAction>
        <RequestOption>Rate</RequestOption>
    </Request>
    <Shipment>
        <Shipper>
            <Name>Name</Name>
            <ShipperNumber></ShipperNumber>
            <Address>
                <AddressLine1>Address Line</AddressLine1>
                <City>Corado</City>
                <PostalCode>00646</PostalCode>
                <CountryCode>PR</CountryCode>
            </Address>
        </Shipper>
        <ShipTo>
            <CompanyName>Company Name</CompanyName>
            <Address>
                <AddressLine1>Address Line</AddressLine1>
                <City>Corado</City>
                <PostalCode>00646</PostalCode>
                <CountryCode>PR</CountryCode>
            </Address>
        </ShipTo>
        <ShipFrom>
            <CompanyName>Company Name</CompanyName>
            <Address>
                <AddressLine1>Address Line</AddressLine1>
                <City>Boca Raton</City>
                <StateProvinceCode>FL</StateProvinceCode>
                <PostalCode>33434</PostalCode>
                <CountryCode>US</CountryCode>
            </Address>
        </ShipFrom>
        <Service>
            <Code>02</Code>
            <Description>2nd Day Air</Description>
        </Service>
        <Package>
            <PackagingType>
                <Code>02</Code>
                <Description>UPS Package</Description>
            </PackagingType>
            <PackageWeight>
                <UnitOfMeasurement>
                    <Code>LBS</Code>
                </UnitOfMeasurement>
                <Weight>15.2</Weight>
            </PackageWeight>
        </Package>
    </Shipment>
</RatingServiceSelectionRequest>

我要将RatingServiceSelectionRequest的这个节点添加到AccessRequest

我想要这样的结果:

<?xml version="1.0"?>
<AccessRequest>
    <AccessLicenseNumber>7D54A69331203795</AccessLicenseNumber>
    <UserId>peterfelts</UserId>
    <Password>abc123</Password>
    <RatingServiceSelectionRequest>
        <Request>
            <RequestAction>Rate</RequestAction>
            <RequestOption>Rate</RequestOption>
        </Request>
        <Shipment>
            <Shipper>
                <Name>Name</Name>
                <ShipperNumber></ShipperNumber>
                <Address>
                    <AddressLine1>Address Line</AddressLine1>
                    <City>Corado</City>
                    <PostalCode>00646</PostalCode>
                    <CountryCode>PR</CountryCode>
                </Address>
            </Shipper>
            <ShipTo>
                <CompanyName>Company Name</CompanyName>
                <Address>
                    <AddressLine1>Address Line</AddressLine1>
                    <City>Corado</City>
                    <PostalCode>00646</PostalCode>
                    <CountryCode>PR</CountryCode>
                </Address>
            </ShipTo>
            <ShipFrom>
                <CompanyName>Company Name</CompanyName>
                <Address>
                    <AddressLine1>Address Line</AddressLine1>
                    <City>Boca Raton</City>
                    <StateProvinceCode>FL</StateProvinceCode>
                    <PostalCode>33434</PostalCode>
                    <CountryCode>US</CountryCode>
                </Address>
            </ShipFrom>
            <Service>
                <Code>02</Code>
                <Description>2nd Day Air</Description>
            </Service>
            <Package>
                <PackagingType>
                    <Code>02</Code>
                    <Description>UPS Package</Description>
                </PackagingType>
                <PackageWeight>
                    <UnitOfMeasurement>
                        <Code>LBS</Code>
                    </UnitOfMeasurement>
                    <Weight>15.2</Weight>
                </PackageWeight>
            </Package>
        </Shipment>
    </RatingServiceSelectionRequest>
</AccessRequest>

任何人都可以帮助我,我该怎么做?

1 个答案:

答案 0 :(得分:2)

恕我直言-最好的方法是将XML导入DOM,这使您可以更灵活地添加节点和移动节点。

此代码将访问请求加载为DOM文档的开头,然后将第二个XML导入为新节点,然后将其附加到现有XML的末尾...

$dom = new DOMDocument();
$dom->loadXML($accessRequestXML->asXML());
$importrr = dom_import_simplexml($rateRequestXml);
$domImportRR = $dom->importNode($importrr, true);
$dom->documentElement->appendChild($domImportRR);
echo $dom->saveXML();