簡體   English   中英

使用domDocument在PHP中進行XML解析

[英]XML Parsing in PHP with domDocument

我有一個看起來像的Xml

<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>

我想獲取所有html標記,因為它們在body標記中。 但是當我使用domDocument獲取html標記時,大多數標記都丟失了。 這是我的代碼在下面

$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>';
        }
    }

我得到的結果是

測試http://ecample.com/bla.jpg您好{{rating}} {{content}}這是一個段落單擊我!這又是一個段落! CODE STUFF CODE STUFF世界! 代碼表代碼表代碼表

您可以在此處看到大多數html都無法在此處使用..請幫助

首先,您必須了解方法getElementsByTagName和其他任何getter返回類DOMNode對象(或對象數組)。 如果它具有內容,但沒有包裝在任何標簽中,則可以通過nodeValue屬性返回此內容。 然后使用它來獲取模板名稱。 但是nodeValue不包含子代的html。 您必須創建它。 這是示例:

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

因此您的代碼應類似於:

$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
    }
}

我只對$template_html進行了更改,但我想您現在可以完成其余工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM