beigeface Posted May 26, 2010 Share Posted May 26, 2010 Hi, I'm not really a web programmy kind of guy, but I have a bit of a problem. I'm trying to read the contents of an XML file on my server, I did a quick search on XML, but nothing seemed to match, if anyone has better search-fu, I apologise for having made a double post and a link would be appreciated The XML I'm messing with is quick and dirty (which might be the problem I don't know) <update> <version>000.001.000.5</version> <url>http://www.XXX.net/XXX.php</url> <fileExtension>0.1.0.5</fileExtension> </update> And what I'm trying to do, is make a copy of which Tag I'm at in the "startElement" function, so that when the "textData" one is called, I can compare it, and store the text data in the appropriate variable - hopefully letting me chose which bit of data from the file I echo, assigning the data into "$elem_name" in "startElement" goes fine, but then when I echo it at the start of "textData" there's nothing in it anymore. My question is - how can I store the data in that variable so that it's not lost after leaving a function? I tried using the global keyword but it changes nothing. <?php $version; $url; $fileExt; $elem_name; function startElement( $parser, $name, $attrs ) { $elem_name = $name; echo "1.$elem_name<br>"; } function endElement( $parser, $name ) { } function textData( $parser, $text) { echo "2. $elem_name <br>"; if ($elem_name == "VERSION") { $version = $text; echo "A.$text <br>"; } if ($elem_name == "URL") { $url = $text; echo "B.$text <br>"; } if ($elem_name == "FILEEXTENSION") { $fileExt = $text; echo "C.$text <br>"; } } $parser = xml_parser_create(); xml_set_element_handler( $parser, "startElement", "endElement" ); xml_set_character_data_handler( $parser, "textData" ); $f = fopen( 'app_version.xml', 'r' ); while( $data = fread( $f, 4096 ) ) { xml_parse( $parser, $data ); } xml_parser_free( $parser ); echo $version; echo $url; echo $fileExt; ?> The echos look like so on the web page: 1.UPDATE 2. 2. 1.VERSION 2. 2. 2. 1.URL 2. 2. 1.FILEEXTENSION 2. 2. What I want is for those number "2s" to read the same as the number 1s above them, or if there's a better way to move through XML tag-by-tag - then I'm up for learning it. Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/202959-xml-parser-newbie-question/ Share on other sites More sharing options...
fja3omega Posted May 26, 2010 Share Posted May 26, 2010 have you tried doing the echo inside the function? Quote Link to comment https://forums.phpfreaks.com/topic/202959-xml-parser-newbie-question/#findComment-1063556 Share on other sites More sharing options...
beigeface Posted May 26, 2010 Author Share Posted May 26, 2010 have you tried doing the echo inside the function? I've tried echos inside and outside, I can't get it to actually echo anything for "$elem_name" anywhere but inside the "startElement" function. Quote Link to comment https://forums.phpfreaks.com/topic/202959-xml-parser-newbie-question/#findComment-1063563 Share on other sites More sharing options...
PFMaBiSmAd Posted May 26, 2010 Share Posted May 26, 2010 If you want to capture and 'return' the tag or data values, you will need to use the xml parser inside of a class/OOP, because the three call back functions are called by the parser and they cannot directly accept or return values to your program. However, in a class, you can store values found in the call back function into class variables. Quote Link to comment https://forums.phpfreaks.com/topic/202959-xml-parser-newbie-question/#findComment-1063565 Share on other sites More sharing options...
beigeface Posted May 26, 2010 Author Share Posted May 26, 2010 If you want to capture and 'return' the tag or data values, you will need to use the xml parser inside of a class/OOP, because the three call back functions are called by the parser and they cannot directly accept or return values to your program. However, in a class, you can store values found in the call back function into class variables. Okay - I'll look into that, thanks for the help. That explains why it's not working then. Quote Link to comment https://forums.phpfreaks.com/topic/202959-xml-parser-newbie-question/#findComment-1063569 Share on other sites More sharing options...
ignace Posted May 26, 2010 Share Posted May 26, 2010 $dom = new DomDocument(); if ($dom->load('path/to/xml.file')) { foreach ($dom->childNodes as $node) { //if ('version' === $node->nodeName) { } } Quote Link to comment https://forums.phpfreaks.com/topic/202959-xml-parser-newbie-question/#findComment-1063570 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.