Jump to content

XMl Parser - Newbie Question


beigeface

Recommended Posts

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 :P 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.  :confused:

 

<?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 :)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.