views:

289

answers:

2

Currently i am using and have looked through this code http://us.php.net/manual/en/function.xml-set-element-handler.php#85970 How do i get the text between tags?

i am using php5 and XML Parser

+2  A: 

This should explain it:

 xml_set_character_data_handler ( $parser, 'tagContent' );

 function tagContent ( $parser, $content )
 {
 }
Lars D
http://us.php.net/manual/en/function.xml-set-character-data-handler.php
Ewan Todd
+1  A: 

Don't use PHP's XML Parser unless you know what you are doing - it's a SAX parser which requires you to understand the idea of SAX events. You don't need it unless you need to handle very large XML files very quickly. For 99% of cases, you don't need to use it. Based on your question, you are simply opening up an XML file and searching for a particular tag and extracting the strings contained in that tag. For that, you should use the DOM or SimpleXML parsers. SimpleXML lets you use XPath - see this example code which does broadly what you want. (If you want to use DOM, you'll have to look for it on the PHP site - I can't post hyperlinks as I'm a newbie - just be careful not to confuse it with DOMXML which is the old PHP4 XML parser.)

If I've misread this and you do actually want to learn how to use SAX parsing, Google will help - but the basics theory is this: your code will be processing the complete document as a series of events. Each event will represent something in the document - starting with the beginning of the document, then each element being opened and closed, each attribute being parsed, and each string being parsed. You need to have code that listens to the events you are interested in - when an element gets opened and closed - and then from that, see if the element that's being opened is the one you want - if it is, you need to then tell the event handler that handles the text that it should now listen for text and store whatever text you are looking for into a variable. When you are done, you can then close the stream off and do whatever you want to do with the text.

Again, based on the question, it sounds like you need DOM or SimpleXML not SAX.

Tom Morris