Page 1 of 1

DomDocument loading

Posted: Mon Feb 15, 2010 3:51 pm
by smitty265
Hi,

I'm fairly new to PHP. I am encountering some behavior which I'm sure translates into some basic hole in my PHP knowledge. Help is greatly appreciated.

Anyway, I'm loading a PHP 5 DomDocument in preparation for use in an xslt transformation. I want to load an xml string that has returned from a function call. In any case, when I do load() and provide the xml (pre-saved from a manual function call) as a file, it works fine. When I attempt to programatically invoke loadXML on the xml string however, the function always fails by returning FALSE.

To narrow it down I tried using small string literals. This fails if (!$xml_obj->loadXML('<root><node></root> '))

This succeeds (resulting in an empty DomDocument) if (!$xml_obj->loadXML(" "))

Is there some basic character escaping I need to do or anything? I'm puzzled but I'm sure there's one simple thing I don't know to do. Any help appreciated.

Thanks,

David

Re: DomDocument loading

Posted: Mon Feb 15, 2010 6:10 pm
by Darhazer
Actually

Code: Select all

<root><node></root>
is not valid XML. All tags should be closed. Try with this one:

Code: Select all

<root><node/></root>

Re: DomDocument loading

Posted: Mon Feb 15, 2010 6:21 pm
by smitty265
Hi,

Thanks for looking closely and the quick reply!

Yes, I had definitely posted bad xml in my question. I fixed it and still failed. The process of doing so made me think of something though.

I started looking at all of the examples again that I had found before via googling. One thing jumped out at me - no one was if test'ing the load method for failure. So, it turns out that if I just say $xml_str.load($xmlStr) it works in that it gets the xml string into the dom document, even though it is returning false.

I don't know why; but I'm past the issue.

Thanks again for the quick reply. As I begin to get further into PHP, I can see that the sense of community around it is really great.

Thanks,

David