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.

UltimateWeapon,

 

You are asking a couple questions that conflict with one another.

 

Try http://us3.php.net/manual/en/simplexmlelement.getname.php for your solution.

 

Conflict ? I have no idea what you mean.

And that page hasn't got much to do with my question either  :)

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:

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);
    }
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.