Q:

PHP DOMDocument: how to incorporate a string of xml?

I am building up an xml file and need to include a segment of xml saved in a database (yeah, I wish that wasn't the case too).  
    // parent element
    $parent = $dom->createElement('RecipeIngredients');

    // the xml string I want to include
    $xmlStr = $row['ingredientSectionXml'];

    // load xml string into domDocument
    $dom->loadXML( $xmlStr );

    // add all Ingredient Sections from xmlStr as children of $parent
    $xmlList = $dom->getElementsByTagName( 'IngredientSection' );
    for ($i = $xmlList->length; --$i >= 0; ) {
      $elem = $xmlList->item($i);
      $parent->appendChild( $elem );
    }

    // add the parent to the $dom doc
    $dom->appendChild( $parent );

Right now, I get the following error when I hit the line $parent->appendChild( $elem );
Fatal error: Uncaught exception 'DOMException' with message 'Wrong Document Error'
The XML in the string might look something like the following example.  An important point is that there may be multiple IngredientSections, all of which need to be appended to the $parent element.
<IngredientSection name="Herbed Cheese">
  <RecipeIngredient>
    <Quantity>2</Quantity>
    <Unit>cups</Unit>
    <Item>yogurt cheese</Item>
    <Note>(see Tip)</Note>
    <MeasureType/>
    <IngredientBrand/>
  </RecipeIngredient>
  <RecipeIngredient>
    <Quantity>2</Quantity>
    <Unit/>
    <Item>scallions</Item>
    <Note>, trimmed and minced</Note>
    <MeasureType/>
    <IngredientBrand/>
  </RecipeIngredient>
<IngredientSection name="Cracked-Wheat Crackers">
</IngredientSection>
  <RecipeIngredient>
    <Quantity>2</Quantity>
    <Unit>teaspoon</Unit>
    <Item>salt</Item>
    <Note/>
    <MeasureType/>
    <IngredientBrand/>
  </RecipeIngredient>
  <RecipeIngredient>
    <Quantity>1 1/4</Quantity>
    <Unit>cups</Unit>
    <Item>cracked wheat</Item>
    <Note/>
    <MeasureType/>
    <IngredientBrand/>
  </RecipeIngredient>
</IngredientSection>

A:

Here a two possible solutions:
Import From A Source Document
This works only if the XML string is a valid document. You need to import the document element, or any descendant of it. Depends on the part you would like to add to the target document.
$xml = "<child>text</child>";

$source = new DOMDocument();
$source->loadXml($xml);

$target = new DOMDocument();
$root = $target->appendChild($target->createElement('root'));
$root->appendChild($target->importNode($source->documentElement, TRUE));

echo $target->saveXml();

Output: 
<?xml version="1.0"?>
<root><child>text</child></root>

Use A Document Fragment
This works for any valid XML fragment. Even if it has no root node.
$xml = "text<child>text</child>";

$target = new DOMDocument();
$root = $target->appendChild($target->createElement('root'));

$fragment = $target->createDocumentFragment();
$fragment->appendXml($xml);
$root->appendChild($fragment);

echo $target->saveXml();

Output:
<?xml version="1.0"?>
<root>text<child>text</child></root>

