I'm kinda new here in stackoverflow so forgive me in advance. :)
I'm trying to merge objects with the same BranchCode and basically, just making the Branch as a child of the main Product node. Please see my sample XML below. Thanks.
I have this XML (simpleXMLElement->asXML()):
    <?xml version="1.0" encoding="utf-8"?>
<ArrayOfProduct xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-02-2166</ProductCode>
    <BranchCode>14</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-02-2166</ProductCode>
    <BranchCode>150</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-02-2166</ProductCode>
    <BranchCode>226</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-02-2166</ProductCode>
    <BranchCode>227</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-02-2166</ProductCode>
    <BranchCode>26</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-02-2166</ProductCode>
    <BranchCode>34</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-02-2166</ProductCode>
    <BranchCode>35</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-02-2166</ProductCode>
    <BranchCode>400A</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-02-2166</ProductCode>
    <BranchCode>405A</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-02-2166</ProductCode>
    <BranchCode>460A</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-02-2166</ProductCode>
    <BranchCode>57</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-02-2166</ProductCode>
    <BranchCode>83</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-02-2166</ProductCode>
    <BranchCode>C3</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-02-2166</ProductCode>
    <BranchCode>Global</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-051-2030</ProductCode>
    <BranchCode>14</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-051-2030</ProductCode>
    <BranchCode>150</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-051-2030</ProductCode>
    <BranchCode>226</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-051-2030</ProductCode>
    <BranchCode>227</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-051-2030</ProductCode>
    <BranchCode>26</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-051-2030</ProductCode>
    <BranchCode>34</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-051-2030</ProductCode>
    <BranchCode>35</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-051-2030</ProductCode>
    <BranchCode>400A</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-051-2030</ProductCode>
    <BranchCode>405A</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-051-2030</ProductCode>
    <BranchCode>460A</BranchCode>
    <Available>5.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-051-2030</ProductCode>
    <BranchCode>57</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-051-2030</ProductCode>
    <BranchCode>83</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-051-2030</ProductCode>
    <BranchCode>C3</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-051-2030</ProductCode>
    <BranchCode>Global</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>5.00</AvailableGlobally>
  </Product>
</ArrayOfProduct>
And I want an output that is similar to this:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfProduct xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-50-3535</ProductCode>
    <Branch>
       <BranchCode>C3</BranchCode>
       <Available>10.00</Available>
       <AvailableCSL>0.00</AvailableCSL>
       <AvailableGlobally>100.00</AvailableGlobally>
    </Branch>
    <Branch>
       <BranchCode>A5</BranchCode>
       <Available>20.00</Available>
       <AvailableCSL>0.00</AvailableCSL>
       <AvailableGlobally>100.00</AvailableGlobally>
    </Branch>
    ....
    ....
  </Product>
  ....
  ....
</ArrayOfProduct>