2

I have 2 xml string, which come from below 2 functions :

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

It products this xml string :

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

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

It products this xml string :

<?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>

I want to add RatingServiceSelectionRequest this node to AccessRequest

I want result like this :

<?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>

Can anyone please help me how can i do this ?

3
  • What do you expect the result to be like (You can miss out most of the XML if this is just copied, but show how the elements should be layered) Commented Dec 3, 2018 at 10:42
  • @NigelRen i have updated my question Commented Dec 3, 2018 at 10:44
  • Possible duplicate of Adding in new XML root node Commented Dec 3, 2018 at 11:10

1 Answer 1

2

IMHO - the best way to do this is to import the XML into DOM which allows you more flexible ways of adding nodes and moving them around.

This code loads the access request as the start of the DOM document and then imports the second XML as a new node, it then appends it to the end of the existing 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();
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.