开发者

PHP parsing xml xpath

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.

I've tried both of these, without success

$description = er开发者_StackOverfloweg_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. 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. If I was to parse the xml to output in the browser then $description = ereg_replace('<description>|</description>','',$person->description->asXML()); works fine... this isn't true though when I'm trying to import into mysql. Do I need to add something to the mysql INSERT? mysql_query("UPDATE table SET description = '$value' WHERE id = '$id'");


Please familiarize yourself with the 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, e.g.

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

$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


I'd like to parse the description so that it returns the html tags that are inside.

In XPath you would select the child nodes of the description elements.

Use:

 "//person/description/*"

to get all child nodes (html tags only) or

 "//person/description/node()"

to get all child nodes (html tags and text nodes).

For instance, this php code:

<?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. Each item is retrieved with all its descendant nodes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜