DomDocument loading

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
smitty265
Forum Newbie
Posts: 2
Joined: Mon Feb 15, 2010 3:40 pm

DomDocument loading

Post 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
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: DomDocument loading

Post 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>
smitty265
Forum Newbie
Posts: 2
Joined: Mon Feb 15, 2010 3:40 pm

Re: DomDocument loading

Post 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
Post Reply