简体   繁体   中英

XML Parsing in PHP with domDocument

I have a Xml which looks like

<theme>
<name>Test</name>
<thumb>http://ecample.com/bla.jpg</thumb>;
<template>
<name>Hello</name>
<html>
<body> 
<div id="hell">
<input type="text" name="text1" id="text1" value="Type Some thing"/>
<input type="button" name="button1" id="button1" value="Button" />

<div class="hello">
<p>here is a paragraph</p>
</div>
<div class="hello123">
    <p><a href="#">Click Me!</a>here is a paragraph again!</p>
</div>
<textarea name="hello"></textarea>
</div>
</body> 
</html>
<css> CODE STUFF </css>
<javascript> CODE STUFF </javascript>
</template>
<template>
<name>World!</name>
<html> CODE STUFF </html>
<css> CODE STUFF </css>
<javascript> CODE STUFF </javascript>
</template>
</theme>

I want to get all html tags as they are in the body tag. but when i get html tag using domDocument most of tags are missing. this is my code below

$doc = new DOMDocument();
    $doc->loadXML( $xml_file_string );//xml file loading here
    $themes = $doc->getElementsByTagName( "theme" );
    foreach( $themes as $theme )
    {
        $theme_name = $theme->getElementsByTagName( "name" );
        $theme_thumb = $theme->getElementsByTagName( "thumb" );
        $theme_name = $theme_name->item(0)->nodeValue;
        $theme_thumb = $theme_thumb->item(0)->nodeValue;
        echo $theme_name.'<br>';
        echo $theme_thumb.'<br>';
        $templates = $theme->getElementsByTagName( "template" );
        foreach( $templates as $template )
        {
            $template_name = $template->getElementsByTagName( "name" );
            $template_name = $template_name->item(0)->nodeValue;
            $template_html = $template->getElementsByTagName( "html" );
            $template_html = $template_html->item(0)->nodeValue;
            $template_css  = $template->getElementsByTagName( "css" );
            $template_css  = $template_css->item(0)->nodeValue;
            $template_javascript = $template->getElementsByTagName( "javascript" );
            $template_javascript = $template_javascript->item(0)->nodeValue;
            echo $template_name.'<br>';
            echo html_entity_decode($template_html).'<br>';
            echo $template_css.'<br>';
            echo $template_javascript.'<br>';
        }
    }

and the result i am getting is like,

Test http://ecample.com/bla.jpg Hello {{rating}} {{content}} here is a paragraph Click Me!here is a paragraph again! CODE STUFF CODE STUFF World! CODE STUFF CODE STUFF CODE STUFF

You can see here that most of html is not working here.. please help

First, you have to understand, that method getElementsByTagName and any other getter return object (or array of objects) of class DOMNode . If it has content, but not wrapped in any tag, this content can be returned by nodeValue property. And you use it to get template name. But nodeValue doesn't contain html of children. You have to create it. Here is example:

$tmp_dom = new DOMDocument(); 
$tmp_dom->appendChild($tmp_dom->importNode($child, true)); 
$html = trim($tmp_dom->saveHTML());

so your code should be like:

$doc = new DOMDocument();
$doc->loadXML( $xml_file_string );//xml file loading here
$themes = $doc->getElementsByTagName( "theme" );
foreach( $themes as $theme )
{
    $theme_name = $theme->getElementsByTagName( "name" );
    $theme_thumb = $theme->getElementsByTagName( "thumb" );
    $theme_name = $theme_name->item(0)->nodeValue;
    $theme_thumb = $theme_thumb->item(0)->nodeValue;
    echo $theme_name.'<br>';
    echo $theme_thumb.'<br>';
    $templates = $theme->getElementsByTagName( "template" );
    foreach( $templates as $template )
    {
        $template_name = $template->getElementsByTagName( "name" );
        $template_name = $template_name->item(0)->nodeValue;
        $template_html = $template->getElementsByTagName( "html" );

        //HERE IS CHANGE
        $tmpHtml = new DOMDocument();
        $tmpHtml->appendChild($tmpHtml->importNode($template_html->item(0), true)); 
        $template_html = trim($tmpHtml->saveHTML());

        //REST OF CODE
    }
}

I've only made change for $template_html , but I think you can now do the rest.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM