1

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 = 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. 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'");

4
  • How are you going about bringing the XML into your PHP script? Commented Aug 3, 2011 at 20:18
  • Your XML is invalid because it is missing a root element. Also note that HTML cannot be expressed with XML because it is based on SGML, not XML (different rules). You are looking for XHTML, but the p elements inside the description element are not XHTML if you dont assign them with an XHTML namespace. They are just elements of whatever XML application that invalid XML is. Commented Aug 3, 2011 at 20:48
  • @Gordon - xml isn't invalid... that is a shortened example outlining the specific issue. Commented Aug 3, 2011 at 20:56
  • @Jeffrey then it is an invalid shortened example outlining the specific issue. Commented Aug 3, 2011 at 20:59

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.