Connecting Tech Pros Worldwide Help | Site Map

XML parser will not return a single element from my XML code

abdoelmasry's Avatar
Member
 
Join Date: Oct 2006
Location: Egypt :: Ismailia
Posts: 87
#1: Sep 24 '07
HI men

im trying To get xml file conetent To insert to database

xml parser functions couldn't get single element from xml file

it's return all start elements , end elements and data elements

i need to create variables have element name and carry element data

then i can get element data by code :

[PHP]echo $(element name) ;[/PHP]

here is my code , it's not working

i don't know what is wrong on it

Any body help PLZ !

[PHP]<?php
$xml_filename="style.xml";

function load_file($file_to_load){
$openxml=fopen($file_to_load,"r") or die("Cann't Open File $file_to_load");
$file_data=fread($openxml,filesize($file_to_load)) ;
return $file_data;
}

function opentag($xml_parser,$starttag){
global $starttag;
}

function closetag($xml_parser,$endtag){
global $endtag;
}

function xml_data($xml_parser,$data){
$$starttag=$data;
}

$xml_parser=xml_parser_create();
xml_set_element_handler($xml_parser,"opentag","clo setag");
xml_set_character_data_handler($xml_parser,"xml_da ta");
xml_parse($xml_parser,load_file($xml_filename));
xml_parser_free($xml_parser);

//every element name variable carry element data;

echo $Elementname;
?>[/PHP]
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#2: Sep 24 '07

re: XML parser will not return a single element from my XML code


Heya, Abdoelmasry.

Changed thread title to better describe the problem (did you know that threads whose titles do not follow the Posting Guidelines actually get FEWER responses?).

Is there any whitespace before the XML declaration in your XML code?

Try trim()ing your content.

Also this function:
Expand|Select|Wrap|Line Numbers
  1. function load_file($file_to_load){
  2. $openxml=fopen($file_to_load,"r") or die("Cann't Open File $file_to_load");
  3. $file_data=fread($openxml,filesize($file_to_load))  ;
  4. return $file_data;
  5. }
  6.  
Is nice, but why reinvent the wheel?
volectricity's Avatar
Expert
 
Join Date: Jun 2007
Location: Baltimore
Posts: 587
#3: Sep 24 '07

re: XML parser will not return a single element from my XML code


If you have PHP5 (which you should!), I'd suggest that you use the DOM. It's much easier and follows the W3C standards.
abdoelmasry's Avatar
Member
 
Join Date: Oct 2006
Location: Egypt :: Ismailia
Posts: 87
#4: Sep 24 '07

re: XML parser will not return a single element from my XML code


Hi pbmods

im sorry for bad title again

i want to set good title but im not good in english

im trying to post good words as i can

thank you for allowance

i tried to trim elements name and change case to lower

but it's not working

the main problem is :

the function GLOBAL not working , i cann't make any public variables

this is not from php.ini , im working with GLOBAL with another script in the same pc , it works good

but some thing wrong with this script

Here Is Modified Code:

[PHP]<?php
$xml_filename="style.xml";

function load_file($file_to_load){
$openxml=fopen($file_to_load,"r") or die("Cann't Open File $file_to_load");
$file_data=fread($openxml,filesize($file_to_load)) ;
return trim($file_data);
}

function opentag($xml_parser,$starttag){
GLOBAL $starttag;
$starttag=trim($starttag);
$starttag=strtolower($starttag);
}
function closetag($xml_parser,$endtag){
GLOBAL $endtag;
$endtag=trim($endtag);
$endtag=strtolower($endtag);
}

function xml_data($xml_parser,$data){
GLOBAL $$starttag;
$$starttag=$data;
}

$xml_parser=xml_parser_create();
xml_set_element_handler($xml_parser,"opentag","clo setag");
xml_set_character_data_handler($xml_parser,"xml_da ta");
xml_parse($xml_parser,load_file($xml_filename));
xml_parser_free($xml_parser);

//every element name variable carry element data;
echo $element_name;
?>[/PHP]

yes volectricity im using php5 , i know DOM Functions
But i think xml parser is faster and uses small memory range

i need advice

thank U
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#5: Sep 24 '07

re: XML parser will not return a single element from my XML code


Heya, abdoelmasry.

Nothing personal; don't worry about it. It's a standard response, and I'm certainly making no judgments. I appreciate the effort, and in return, I will do what I can to make sure that as many people can find your post as possible :)

In this function:
Expand|Select|Wrap|Line Numbers
  1. function xml_data($xml_parser,$data){
  2.  GLOBAL $$starttag;
  3. $$starttag=$data;
  4. }
  5.  
Did you mean to do this, instead:
Expand|Select|Wrap|Line Numbers
  1. function xml_data($xml_parser,$data){
  2.  global $starttag;
  3. $starttag=$data;
  4. }
  5.  
abdoelmasry's Avatar
Member
 
Join Date: Oct 2006
Location: Egypt :: Ismailia
Posts: 87
#6: Sep 26 '07

re: XML parser will not return a single element from my XML code


thank u pbmods
for everything

im realy mean this:

[PHP]function xml_data($xml_parser,$data){
GLOBAL $$starttag;
$$starttag=$data;
}[/PHP]

because

i want to set element name as variable

ex:

if the element name is : login_form

the variable will be:

[PHP]$login_form= login form html code(element contents)[/PHP]

then i can print login form by writing :

[PHP]echo $login_form[/PHP]

i don't know what is wrong with function GLOBAL

when i remove GLOBAL The code works good but as you know

i can't read variables out of the function

i mean i can't make any variable as global
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#7: Sep 26 '07

re: XML parser will not return a single element from my XML code


Heya, abdoelmasry.

Gotcha.

Using 'global' instead of 'GLOBAL' is a tad bit faster, as then PHP doesn't have to convert it to lowercase.

The only problem is that you don't define $starttag in your function. In other words, $$starttag === ${null}, which is probably not what you meant.

Perhaps you meant to do something like this:
Expand|Select|Wrap|Line Numbers
  1. function register_global($starttag, $data)
  2. {
  3.     global $$starttag;
  4.     $$starttag = $data;
  5. }
  6.  
abdoelmasry's Avatar
Member
 
Join Date: Oct 2006
Location: Egypt :: Ismailia
Posts: 87
#8: Sep 27 '07

re: XML parser will not return a single element from my XML code


Hi pbmods

i tried but it's not working

i gonna be mad

it's not important code in my work but

i wanna know WHAT IS THE WRONG WITH THIS CODE ???? !i!i!i!i!i!i!i!i!i!i!i!i!i!i!

[PHP]<?php
$xml_filename="style.xml";

function register_global($tagvar,$data)
{
global $$tagvar;
$$tagvar=$data;
}

function load_file($file_to_load){
$openxml=fopen($file_to_load,"r") or die("Cann't Open File $file_to_load");
$file_data=fread($openxml,filesize($file_to_load)) ;
return $file_data;
}

function opentag($xml_parser,$starttag){
global $starttag;
$starttag=trim($starttag);
$starttag=strtolower($starttag);
}

function closetag($xml_parser,$endtag){
}

function xml_data($xml_parser,$data){
global $starttag;
register_global($starttag,$data);
}

$xml_parser=xml_parser_create();
xml_set_element_handler($xml_parser,"opentag","clo setag");
xml_set_character_data_handler($xml_parser,"xml_da ta");
xml_parse($xml_parser,load_file($xml_filename));
xml_parser_free($xml_parser);

//every element name variable carry element data;
echo $login_form; //To view login form
?>[/PHP]

waiting For Idea ............
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#9: Sep 27 '07

re: XML parser will not return a single element from my XML code


Heya, Abdoelmasry.

Go back and re-read my last post.

The problem is that $starttag is null in your xml_data() function. I used the name 'register_global' because that better describes what the function does, instead of 'xml_data'. But I meant the same function.
abdoelmasry's Avatar
Member
 
Join Date: Oct 2006
Location: Egypt :: Ismailia
Posts: 87
#10: Sep 27 '07

re: XML parser will not return a single element from my XML code


Hi man

i got it

the code works good

but some thing wrong with xml tags

in xml file there is two tags for any element : open , close

the function reads open tag then set it as variable , it carry the element data but when it read close tag ... this is the problem

close tag not carring any data soooo

the function reset the variable with null

i make another code to read elements it works great

i just send tag name and it returns array of all elements , its data

here is my new code :

[PHP]<?php
$xml_style_file="style.xml";

## File Loader ##
function load_file($file_to_load){
$openxml=fopen($file_to_load,"r") or die("Cann't Open File $file_to_load");
$file_data=fread($openxml,filesize($file_to_load)) ;
return $file_data;
}

## XML element splitter ##
function get_tage_elements($tag_name,$xml_array){
// Get Start , end ofest
while(list($key,$value)=each($xml_array)){
if(($value["tag"]=="$tag_name") and ($value["type"]=="open")){
$startofest=key($xml_array);
}
if(($value["tag"]=="$tag_name") and ($value["type"]=="close")){
$endofest=key($xml_array);
}
}
unset($key,$value);
//get tag slice
$slice_size=$endofest-$startofest;
$tagarray=array_slice($xml_array,$startofest,$slic e_size);
//create elements array
while(list($key,$value)=each($tagarray)){
if($value["type"]=="complete"){
$tagcontents[$value["tag"]]["name"]=$value["tag"];
$tagcontents[$value["tag"]]["data"]=$value["value"];
}}
unset($key,$value);
return $tagcontents;
}

$xml_parser=xml_parser_create();
xml_parse_into_struct($xml_parser,load_file($xml_s tyle_file),$struct);
xml_parse($xml_parser,load_file($xml_style_file));
xml_parser_free($xml_parser);


//Get all elements on tag FORMS

$forms=get_tage_elements("FORMS",$struct);

?>[/PHP]


thank U Great man
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#11: Sep 27 '07

re: XML parser will not return a single element from my XML code


Heya Abdoelmasry.

Change these line:
Expand|Select|Wrap|Line Numbers
  1. xml_parse_into_struct($xml_parser,load_file($xml_style_file),$struct);
  2. xml_parse($xml_parser,load_file($xml_style_file)); 
  3.  
To this:
Expand|Select|Wrap|Line Numbers
  1. xml_parse_into_struct( $xml_parser, load_file($xml_style_file), $values, $index );
  2.  
To access any element of the XML document, you would use this syntax:
Expand|Select|Wrap|Line Numbers
  1. echo $values[$index['TAGNAME'][0]];
  2.  
You can use these statements to get a better idea of what xml_parse_into_struct() does:
Expand|Select|Wrap|Line Numbers
  1. print_r($values);
  2. print_r($index);
  3.  
abdoelmasry's Avatar
Member
 
Join Date: Oct 2006
Location: Egypt :: Ismailia
Posts: 87
#12: Sep 29 '07

re: XML parser will not return a single element from my XML code


Ya man

it works Great

thank you

im sorry for big thread
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#13: Sep 29 '07

re: XML parser will not return a single element from my XML code


Heya, Abdoelmasry.

That's what we're here for ~_^
Reply