简体   繁体   English

如何使用带有 html 标签的 php 解析 XML 标签内的 XML?

[英]How to parse XML using php with html tag inside XML tags?

<?xml version="1.0" encoding="utf-8"?>

<activities>

<activity category="Team">
<date><h4>Date: January 15, 2017</h4></date>
 <work> 
    <li><b>MR X</b> working on X</li>
    <li><b>MR Y</b> working on Y</li></li>
    <li><b>MR Z</b> working on Z</li></li>
 </work>
 </activity>

 </activities>

The above one is the XML file.上面一个是XML文件。 Suppose the file name is activities.xml.假设文件名为activity.xml。

Now below is the PHP code:现在下面是PHP代码:

<?php
$xmls=simplexml_load_file("activities.xml") or die("Error: Cannot create object");

foreach ($xmls as $xml) {
  echo $xml->date;
  echo $xml->work;
}
?>

I want to get the date and work details with <h4> tag and <li> tag and others so that I can work with them in HTML directly.我想使用<h4>标签和<li>标签等获取日期和工作详细信息,以便我可以直接在 HTML 中使用它们。

Is there a way to do that?有没有办法做到这一点?

There is two extra </li> which causes a parse error, except that everythinhg seems good for what you want to do with.有两个额外的</li>会导致解析错误,除了 everythinhg 似乎适合您想要做的事情。 Here is the object I obtain :这是我获得的对象:

SimpleXMLElement Object
(
    [activity] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [category] => Team
                )

            [date] => SimpleXMLElement Object
                (
                    [h4] => Date: January 15, 2017
                )

            [work] => SimpleXMLElement Object
                (
                    [li] => Array
                        (
                            [0] => SimpleXMLElement Object
                                (
                                    [b] => MR X
                                )

                            [1] => SimpleXMLElement Object
                                (
                                    [b] => MR Y
                                )

                            [2] => SimpleXMLElement Object
                                (
                                    [b] => MR Z
                                )

                        )

                )

        )

)    
if(file_exists('activities.xml')) {
    try{
        $xml = simplexml_load_file('activities.xml') or die('Can not load');

        $date = $xml->{activity}->{date}->{h4};
        echo $date.'<br/>';
        foreach($xml->{activity}->{work}->{li} as $work){
             echo $work->{b};
             echo $work.'<br/>';
        }

    }catch (\Exception $e){
        echo $e->getMessage();
    }

}

And proper activities.xml file content is而正确的activity.xml文件内容是

<?xml version="1.0" encoding="utf-8"?>
<activities>
    <activity category="Team">
        <date><h4>Date: January 15, 2017</h4></date>
        <work>
            <li><b>MR X</b> working on X</li>
            <li><b>MR Y</b> working on Y</li>
            <li><b>MR Z</b> working on Z</li>
        </work>
    </activity>
</activities>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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