Jump to content

Converting part of DOM / XML document to a String


UltimateWeapon

Recommended Posts

I have the following DOM document ( this is a sample document and is "as is", it's 100% intended to look like this ):

 

<everything>
<cars>
     <car>ladia</car>
     <car>ferrari</car>
     <garage>big blue building</garage>
      i also have a McLaren F1 but i wont brag about it
</cars>
<houses>
   <house>Big Red House</house>
</houses>
</everything>

 

This is all being put into a

$newdoc = new DOMDocument();

 

$myString = ................. ; // ????? I only want the content of <cars> in here, and without having the <cars> tag

 

 

How can I make this "$myString look like the following:

 

$myString = '

    <car>ladia</car>

    <car>ferrari</car>

    <garage>big blue building</garage>

      i also have a McLaren F1 but i wont brag about it

';

 

 

 

Preferably without any looping.

Link to comment
Share on other sites

Welcome to PHPFreaks, UltimateWeapon. :)

 

This seems like an odd request, and an even odder example file to work with, but I'll humour you.  Here's a snippet which, using DOM as you wanted, takes the source XML and, via a DOMDocumentFragment (which many people have never even heard of, so don't worry if you haven't), outputs the content of that <cars> element.  The important stuff is overly-commented within the if() block.

 

$xml = '
<everything>
<cars>
     <car>ladia</car>
     <car>ferrari</car>
     <garage>big blue building</garage>
      i also have a McLaren F1 but i wont brag about it
</cars>
<houses>
   <house>Big Red House</house>
</houses>
</everything>
';

$newdoc = new DOMDocument();
$newdoc->loadXML($xml);

$cars = $newdoc->getElementsByTagName('cars');

$myString = '';
if ($cars->length > 0) {
    // Get the first cars element
    $cars = $cars->item(0);
    // Create a new, empty DOMDocumentFragment
    $fragment = $newdoc->createDocumentFragment();
    // Loop over cars childNodes and add a copy to the fragment
    foreach ($cars->childNodes as $child) {
        $fragment->appendChild($child->cloneNode(true));
    }
    // Save fragment as an XML string
    $myString = $newdoc->saveXML($fragment);
}

var_dump($myString);

 

To read:

Link to comment
Share on other sites

By the way, my snippet could be simplified since you only want a string (I usually want to play around with the DOMDocumentFragment some more).

 

$myString = '';
if ($cars->length > 0) {
    foreach ($cars->item(0)->childNodes as $child) {
        $myString .= $newdoc->saveXML($child);
    }
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.