XML parser will not return a single element from my XML code  | Member | | Join Date: Oct 2006 Location: Egypt :: Ismailia
Posts: 87
| | |
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]
|  | Site Moderator | | Join Date: Apr 2007 Location: Texas
Posts: 5,435
| | | 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: -
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;
-
}
-
Is nice, but why reinvent the wheel? |  | Expert | | Join Date: Jun 2007 Location: Baltimore
Posts: 587
| | | 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.
|  | Member | | Join Date: Oct 2006 Location: Egypt :: Ismailia
Posts: 87
| | | 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
|  | Site Moderator | | Join Date: Apr 2007 Location: Texas
Posts: 5,435
| | | 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: -
function xml_data($xml_parser,$data){
-
GLOBAL $$starttag;
-
$$starttag=$data;
-
}
-
Did you mean to do this, instead: -
function xml_data($xml_parser,$data){
-
global $starttag;
-
$starttag=$data;
-
}
-
|  | Member | | Join Date: Oct 2006 Location: Egypt :: Ismailia
Posts: 87
| | | 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
|  | Site Moderator | | Join Date: Apr 2007 Location: Texas
Posts: 5,435
| | | 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: -
function register_global($starttag, $data)
-
{
-
global $$starttag;
-
$$starttag = $data;
-
}
-
|  | Member | | Join Date: Oct 2006 Location: Egypt :: Ismailia
Posts: 87
| | | 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 ............
|  | Site Moderator | | Join Date: Apr 2007 Location: Texas
Posts: 5,435
| | | 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.
|  | Member | | Join Date: Oct 2006 Location: Egypt :: Ismailia
Posts: 87
| | | 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
|  | Site Moderator | | Join Date: Apr 2007 Location: Texas
Posts: 5,435
| | | re: XML parser will not return a single element from my XML code
Heya Abdoelmasry.
Change these line: -
xml_parse_into_struct($xml_parser,load_file($xml_style_file),$struct);
-
xml_parse($xml_parser,load_file($xml_style_file));
-
To this: -
xml_parse_into_struct( $xml_parser, load_file($xml_style_file), $values, $index );
-
To access any element of the XML document, you would use this syntax: -
echo $values[$index['TAGNAME'][0]];
-
You can use these statements to get a better idea of what xml_parse_into_struct() does: -
print_r($values);
-
print_r($index);
-
|  | Member | | Join Date: Oct 2006 Location: Egypt :: Ismailia
Posts: 87
| | | 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
|  | Site Moderator | | Join Date: Apr 2007 Location: Texas
Posts: 5,435
| | | re: XML parser will not return a single element from my XML code
Heya, Abdoelmasry.
That's what we're here for ~_^
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,295 network members.
|