Page 1 of 1
help with PHP XML
Posted: Tue Nov 09, 2010 4:06 am
by jdoe
Hi all,
I am having some difficulty. I have the following XML:
<Comment>
<p>test <strong>test</strong></p>
</Comment>
I want to write the code to extract the content within of the <Comment> tags (EXCLUDING the tags).
I want my content to be "<p>test <strong>test</strong></p>".
I tried the following 2 methods, but not what I wanted.
Method 1. The <Comment> tags is INCLUDED.
$nodeComment = $doc->getElementsByTagName("Comment")->item(0);
$strContent= $doc->saveXML($nodeComment);
The content is "<Comment> <p>test <strong>test</strong></p></Comment>". How can I get the content without the <Comment> tags.
Method 2.
$strContent= $doc->getElementsByTagName("Comment")->item(0)->nodeValue;
This content returned is "test test". I need the <p> and <strong> tags in my content.
Can you please help me? Thanks.
Re: help with PHP XML
Posted: Tue Nov 09, 2010 5:13 am
by yacahuma
Did you use htmlentities in your output? If you are testing your output on html you are not going to see the <p>.
Re: help with PHP XML
Posted: Tue Nov 09, 2010 7:01 am
by jdoe
I tried with htmlentities like below. Result still the same.
$strContent= htmlentities($doc->getElementsByTagName("Comment")->item(0)->nodeValue);
Re: help with PHP XML
Posted: Tue Nov 09, 2010 7:58 am
by saltwine
Hi jdoe
Firstly, it's not strictly legal XML what it looks like you're trying to do - putting HTML within a node.
Use CDATA so your string is not parsed:
Code: Select all
<Comment>
<![CDATA[ <p>test <strong>test</strong></p> ]]>
</Comment>
In the past I've managed to pull in XML feeds using the PHP DOMDocument class, and then extract their contents in the following way:
Code: Select all
$doc = new DOMDocument();
$doc->load('http://www.someaddress.com/some.xml');
$xml = simplexml_import_dom($doc);
// Then in your case to get the contents of Comment without the tags
echo strip_tags($xml->Comment);
It's not clear what you're trying to do, but worth mentioning that if you have the XML as a string you just want to parse, you can do it a little simpler using simplexml_load_string (see
http://php.net/manual/en/function.simpl ... string.php).
Re: help with PHP XML
Posted: Tue Nov 09, 2010 6:10 pm
by jdoe
Hi saltwine,
Thanks for your feedback. Just to update you on what I am trying to do.
I am creating a feedback form that will capture user input as comment. When user click on Submit button,
Javascript will create the XML that will contain this user comment (hence in COMMENT tags) and send to the server as AJAX call.
At the server, my PHP would then extract the contents of the COMMENT tags and save it to the database.
I am using WMD editor, which contains HTML formatting tags. So my problem is when I extract the content, as described previously, the formatting tags seems to have "disappeared".
Or is there a better way to do things?
Thanks.
Re: help with PHP XML
Posted: Wed Nov 10, 2010 5:56 am
by saltwine
Hi jdoe
If you're using JavaScript, then you might want to consider JavaScript Object Notation (JSON, pronounced Jason) instead of XML.
However, I just had a quick look at WMD and it might not be as straightforward. From the demo I ran, the code that WMD places in the textarea uses another form of notation that isn't HTML. Instead is parses it and puts the true HTML in the preview window underneath the textarea in a div with id wmd-preview.
Given this, using a Javascript framework such as MooTools or jQuery, you could add a click event to a HTML input button which would send POST data to your PHP script:
Code: Select all
$('your_input_button').addEvent('click', function(e) {
e.stop();
new Request({
url: 'http://your.php.script',
data: {
comment: $('wmd-preview').innerHTML
},
onSuccess: function() {
// Notify user the submission has been made
}
}).post();
);
By writing a function like this, your PHP script at
http://your.php.script will then receive POST data with a comment index which should contain the HTML you're looking for.