Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: combining xml

  1. #1
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,517
    Thanks
    114
    Thanked 110 Times in 109 Posts

    combining xml

    if i had two xml documents

    1:
    Code:
    <products>
      <product>
      </product>
      <product>
      </product>
    </products>
    and 2:
    Code:
    <products>
      <product>
      </product>
      <product>
      </product>
    </products>
    and i wanted to combine them into one xml document

    Code:
    <products>
      <product>
      </product>
      <product>
      </product>
      <product>
      </product>
      <product>
      </product>
    </products>
    So that all the product tags were nested between one products tag how would i go about it. I know how to make
    Code:
    <products>
      <product>
      </product>
      <product>
      </product>
    </products>
    <products>
      <product>
      </product>
      <product>
      </product>
    </products>
    but really i want only one parents. Finding it difficult to get my head around the php documentation on DOM.
    You can not say you know how to do something, until you can teach it to someone else.

  • #2
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Code:
    <?php
    
    $string1 = '<products>
      <product1>
      </product>
      <product2>
      </product>
    </products>';
    
    $string2 = '<products>
      <product3>
      </product>
      <product4>
      </product>
    </products>';
    
    $input = preg_replace('#\r\n|\n#', '', $string1.$string2);
    $output = preg_match_all('#<products>(.+?)</products>#', $input, $matches);
    
    print('<products>'."\n".implode("\n", $matches[1])."\n".'</products>'."\n");
    
    ?>

    That would need a bit of tweaking, but should cover the basic principle.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •