How to Add XML to XML file in PHP

I have this XML file content:

<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2">
    
    <cbc:UBLVersionID>2.1</cbc:UBLVersionID>
    <cbc:ProfileID>reporting:1.0</cbc:ProfileID>
    <cbc:ID>89</cbc:ID>
    <cbc:UUID>a5b20593-3a25-4501-bc5a-04eb87069c46</cbc:UUID>
    <cbc:IssueDate>2022-04-19</cbc:IssueDate>
    <cbc:IssueTime>11:57:00</cbc:IssueTime>
    <cbc:InvoiceTypeCode name="0111010">388</cbc:InvoiceTypeCode>
    <cbc:Note>مبيعات الجوال</cbc:Note>
    <cbc:DocumentCurrencyCode>SAR</cbc:DocumentCurrencyCode>
    <cbc:TaxCurrencyCode>SAR</cbc:TaxCurrencyCode>
    <cac:ContractDocumentReference>
        <cbc:ID>1</cbc:ID>
    </cac:ContractDocumentReference>
    <cac:AdditionalDocumentReference>
        <cbc:ID>ICV</cbc:ID>
        <cbc:UUID>1</cbc:UUID>
    </cac:AdditionalDocumentReference>
    <cac:AdditionalDocumentReference>
        <cbc:ID>PIH</cbc:ID>
        <cac:Attachment>
            <cbc:EmbeddedDocumentBinaryObject mimeCode="text/plain">NWZlY2ViNjZmZmM4NmYzOGQ5NTI3ODZjNmQ2OTZjNzljMmRiYzIzOWRkNGU5MWI0NjcyOWQ3M2EyN2ZiNTdlOQ==</cbc:EmbeddedDocumentBinaryObject>
        </cac:Attachment>
    </cac:AdditionalDocumentReference>

</Invoice>

and want to add this XML file content under Invoice:

    <ext:UBLExtensions>
        <ext:UBLExtension>

        </ext:UBLExtension>
    </ext:UBLExtensions>

And this file content under </cac:AdditionalDocumentReference>:

    <cac:AdditionalDocumentReference>
        <cbc:ID>QR</cbc:ID>
        <cac:Attachment>
            <cbc:EmbeddedDocumentBinaryObject mimeCode="text/plain">ARnYp9mE2YHYsdi5INin2YTYsdim2YrYs9mKAg8zMDA0NDczMjgxMDAwMzMDFDIwMjItMDQtMTlUMTE6NTc6MDBaBAYxNTMuNzAFBTIzLjcwBixoMmpNWHFnd05TeEdCY0JCeWJkcUQwb3k1MFNoNlRPQzFoRnZ6L2hONldZPQdYMFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEYYMMoOaFYAhMO/steotfZyavr6p11SSlwsK9azmsLY7b1b+FLhqMArhB2dqHKboxqKNfvkKDePhpqjui5hcn0QghANswMm535QvQd/8xuP6t5MyLO1BSrjVQbMB3+E/8DGdECSB2ABXdBDP9qEvus/nj7v63yoBwq+dT6gSBflIyAbKlDg==</cbc:EmbeddedDocumentBinaryObject>
        </cac:Attachment>
     </cac:AdditionalDocumentReference>
      <cac:Signature>
      <cbc:ID>urn:oasis:names:specification:ubl:signature:Invoice</cbc:ID>
      <cbc:SignatureMethod>urn:oasis:names:specification:ubl:dsig:enveloped:xades</cbc:SignatureMethod>
      </cac:Signature>

the file will be :

<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2">
    <ext:UBLExtensions>
       <ext:UBLExtension>
       </ext:UBLExtension>
    </ext:UBLExtensions>
    <cbc:UBLVersionID>2.1</cbc:UBLVersionID>
    <cbc:ProfileID>reporting:1.0</cbc:ProfileID>
    <cbc:ID>89</cbc:ID>
    <cbc:UUID>a5b20593-3a25-4501-bc5a-04eb87069c46</cbc:UUID>
    <cbc:IssueDate>2022-04-19</cbc:IssueDate>
    <cbc:IssueTime>11:57:00</cbc:IssueTime>
    <cbc:InvoiceTypeCode name="0111010">388</cbc:InvoiceTypeCode>
    <cbc:Note>مبيعات الجوال</cbc:Note>
    <cbc:DocumentCurrencyCode>SAR</cbc:DocumentCurrencyCode>
    <cbc:TaxCurrencyCode>SAR</cbc:TaxCurrencyCode>
    <cac:ContractDocumentReference>
        <cbc:ID>1</cbc:ID>
    </cac:ContractDocumentReference>
    <cac:AdditionalDocumentReference>
        <cbc:ID>ICV</cbc:ID>
        <cbc:UUID>1</cbc:UUID>
    </cac:AdditionalDocumentReference>
    <cac:AdditionalDocumentReference>
        <cbc:ID>PIH</cbc:ID>
        <cac:Attachment>
            <cbc:EmbeddedDocumentBinaryObject mimeCode="text/plain">NWZlY2ViNjZmZmM4NmYzOGQ5NTI3ODZjNmQ2OTZjNzljMmRiYzIzOWRkNGU5MWI0NjcyOWQ3M2EyN2ZiNTdlOQ==</cbc:EmbeddedDocumentBinaryObject>
        </cac:Attachment>
    </cac:AdditionalDocumentReference>
    <cac:AdditionalDocumentReference>
        <cbc:ID>QR</cbc:ID>
        <cac:Attachment>
            <cbc:EmbeddedDocumentBinaryObject mimeCode="text/plain">ARnYp9mE2YHYsdi5INin2YTYsdim2YrYs9mKAg8zMDA0NDczMjgxMDAwMzMDFDIwMjItMDQtMTlUMTE6NTc6MDBaBAYxNTMuNzAFBTIzLjcwBixoMmpNWHFnd05TeEdCY0JCeWJkcUQwb3k1MFNoNlRPQzFoRnZ6L2hONldZPQdYMFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEYYMMoOaFYAhMO/steotfZyavr6p11SSlwsK9azmsLY7b1b+FLhqMArhB2dqHKboxqKNfvkKDePhpqjui5hcn0QghANswMm535QvQd/8xuP6t5MyLO1BSrjVQbMB3+E/8DGdECSB2ABXdBDP9qEvus/nj7v63yoBwq+dT6gSBflIyAbKlDg==</cbc:EmbeddedDocumentBinaryObject>
        </cac:Attachment>
     </cac:AdditionalDocumentReference>
      <cac:Signature>
      <cbc:ID>urn:oasis:names:specification:ubl:signature:Invoice</cbc:ID>
      <cbc:SignatureMethod>urn:oasis:names:specification:ubl:dsig:enveloped:xades</cbc:SignatureMethod>
      </cac:Signature>
</Invoice>

How can I add them in that order?

Leave a Comment