0

I'm trying to parse an xml document with PHP. I've been using simplexml which has worked up until this last part where it is trying to parse an xml field that has basically an html webpage in it. I don't have the ability to adjust the xml document so this is what i'm working with.

       <DataContent>

<!-- start embedded XHTML document -->

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:xn="http://www.xmlnews.org/ns/">

<head>
<meta content="text/html" http-equiv="Content-Type" charset="UTF-8"/>
<title>title of page</title>

etc...

</body>
</html>

<!-- end embedded XHTML document -->

        </DataContent>

So i go to parse it and it doesn't parse any of it. Anyway to grab the entire DataContent field?

Trying this does not work

$BodyContent=$xml->Item->Component->ContentItem->DataContent;
5
  • Isn't there supposed to be a CDATA flag before stuff like that in an XML doc? Commented Feb 3, 2012 at 21:02
  • Just to be clear, the <body> tag is actually missing in the xml? Commented Feb 3, 2012 at 21:03
  • The body tag is in the xml i just cut it out as there was a large amount of code between the opening and closing. I don't know if there is suppose to be a CDATA tag but i'm not able to adjust it? Commented Feb 3, 2012 at 21:19
  • RiverC, i added a CDATA tag to a test version of the XML and that worked.. so i suppose i'm just going to have to edit the XML code as it comes in. Thanks for the help!!! Commented Feb 3, 2012 at 21:22
  • If you don't wish to edit the incoming XML, then search SO for php innerxml. Commented Feb 4, 2012 at 14:01

1 Answer 1

1

When you call simplexml_load_file or simplexml_load_string set the LIBXML_NOCDATA option and wrapping the DataContent in <![CDATA[...]]> tags. After doing this, you will be able to access the information like you wanted to.

Example:

$xml = simplexml_load_file('/path/to/file.xml', 'SimpleXMLElement', LIBXML_NOCDATA);
Sign up to request clarification or add additional context in comments.

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.