简体   繁体   English

PHP解析xml xpath

[英]PHP parsing xml xpath

XML XML

<person>
    <description>
        <p>blah blah blah</p>
        <p>kjdsfksdjf</p>
    </description>
</person>
<person>
    <description>
        k kjsdf kk sak kfsdjk sadk
    </description>
</person>

I'd like to parse the description so that it returns the html tags that are inside. 我想解析描述,以便它返回其中的html标记。

I've tried both of these, without success 我尝试了这两种方法,但均未成功

$description = ereg_replace('<description>|</description>','',$person->description->asXML());

$description = $person->description;

Any suggestions? 有什么建议么?

EDIT 编辑

What I'm trying to accomplish is to import an xml file into a mysql db. 我要完成的工作是将xml文件导入到mysql数据库中。 Everything is working accept what is mentioned above... the paragraph tags inside the description aren't showing up... and they need to be there. 一切正常,都可以接受上面提到的内容...说明中的段落标记未显示...并且它们必须存在。 The mysql field "description" is set as a text field. mysql字段“描述”设置为文本字段。 If I was to parse the xml to output in the browser then $description = ereg_replace('<description>|</description>','',$person->description->asXML()); 如果我要解析XML以在浏览器中输出,则$description = ereg_replace('<description>|</description>','',$person->description->asXML()); works fine... this isn't true though when I'm trying to import into mysql. 工作正常...这不是真的,但是当我尝试导入mysql时。 Do I need to add something to the mysql INSERT? 我需要添加一些东西到mysql INSERT吗? mysql_query("UPDATE table SET description = '$value' WHERE id = '$id'");

Please familiarize yourself with the SimpleXml API: 请熟悉SimpleXml API:

$xml = <<< XML
<person>
    <description>
        <p>blah blah blah</p>
        <p>kjdsfksdjf</p>
    </description>
</person>
XML;

$person = simplexml_load_string($xml);
foreach ($person->description->children() as $child) {
    echo $child->asXml();
}

gives

<p>blah blah blah</p><p>kjdsfksdjf</p>

Note that SimpleXml isnt capable of doing the same for the second description element you show because it has no concept of text nodes, eg 请注意,SimpleXml无法对您显示的第二个description元素执行相同的操作,因为它没有文本节点的概念,例如

$xml = <<< XML
<person>
    <description>
        k kjsdf kk sak kfsdjk sadk
    </description>
</person>
XML;
$person = simplexml_load_string($xml);
foreach ($person->description->children() as $child) {
    echo $child->asXml();
}

will return an empty string. 将返回一个空字符串。 If you want a unified API, use DOM: 如果要使用统一的API,请使用DOM:

$xml = <<< XML
<people>
    <person>
        <description>
            <p>blah blah blah</p>
            <p>kjdsfksdjf</p>
        </description>
    </person>
    <person>
        <description>
            k kjsdf kk sak kfsdjk sadk
        </description>
    </person>
</people>
XML;

$dom = new DOMDocument;
$dom->loadXml($xml);
$xp = new DOMXPath($dom);
foreach ($xp->query('/people/person/description/node()') as $child) {
    echo $dom->saveXml($child);
}

will give 会给

        <p>blah blah blah</p>
        <p>kjdsfksdjf</p>

        k kjsdf kk sak kfsdjk sadk

For importing XML into MySql, you can also use http://dev.mysql.com/doc/refman/5.5/en/load-xml.html 要将XML导入MySql,还可以使用http://dev.mysql.com/doc/refman/5.5/en/load-xml.html

I'd like to parse the description so that it returns the html tags that are inside. 我想解析描述,以便它返回其中的html标记。

In XPath you would select the child nodes of the description elements. 在XPath中,您将选择description元素的子节点。

Use: 采用:

 "//person/description/*"

to get all child nodes (html tags only) or 获取所有子节点(仅html标记)或

 "//person/description/node()"

to get all child nodes (html tags and text nodes). 获取所有子节点(html标记和文本节点)。

For instance, this php code: 例如,此php代码:

<?php
$xml = simplexml_load_file("test.xml");

$result = $xml->xpath("//person/description/*");

print_r($result);
?>

Returns an array of SimpleXMLElements which are children of description . 返回SimpleXMLElements的数组,这些数组是description Each item is retrieved with all its descendant nodes. 检索每个项目及其所有后代节点。

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

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