473,394 Members | 1,902 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

abdoelmasry
104 100+
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]
Sep 24 '07 #1
12 1958
pbmods
5,821 Expert 4TB
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?
Sep 24 '07 #2
kovik
1,044 Expert 1GB
If you have PHP5 (which you should!), I'd suggest that you use the DOM. It's much easier and follows the W3C standards.
Sep 24 '07 #3
abdoelmasry
104 100+
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
Sep 24 '07 #4
pbmods
5,821 Expert 4TB
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.  
Sep 24 '07 #5
abdoelmasry
104 100+
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
Sep 25 '07 #6
pbmods
5,821 Expert 4TB
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.  
Sep 25 '07 #7
abdoelmasry
104 100+
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 ............
Sep 27 '07 #8
pbmods
5,821 Expert 4TB
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.
Sep 27 '07 #9
abdoelmasry
104 100+
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
Sep 27 '07 #10
pbmods
5,821 Expert 4TB
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.  
Sep 27 '07 #11
abdoelmasry
104 100+
Ya man

it works Great

thank you

im sorry for big thread
Sep 29 '07 #12
pbmods
5,821 Expert 4TB
Heya, Abdoelmasry.

That's what we're here for ~_^
Sep 29 '07 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: annoyingmouse2002 | last post by:
Hi there, sorry if this a long post but I'm really just starting out. I've been using MSXML to parse an OWL but would like to use a different solution. Basically it reads the OWL (Based on XML)...
1
by: Patrick Gunia | last post by:
Hi, i´m trying to build a xml - parser, which should simply list all used tokens an dattributes including their values. So far, so good, this works, but now i try to check for illegal phrases in...
3
by: cr88192 | last post by:
for various reasons, I added an imo ugly hack to my xml parser. basically, I wanted the ability to have binary payload within the xml parse trees. this was partly because I came up with a binary...
4
by: Greg B | last post by:
Well since getopt() doesn't seem to be compatible with Windows, and the free implementation of it for Windows that I found still had some annoying restrictions, I thought I'd whip up a simple...
14
by: Simon Morgan | last post by:
I'm trying to write a function to parse a Reverse Polish Notation string from stdin and return 1 token at a time. For those of you who are unaware an RPN string looks like this: 1 2 + 4 * 3 + ...
9
by: Doug | last post by:
Hi I have a method that I would like to return the value to the calling event. I have a bad example below that doesnt give me what I want. What I want is to have a my readxml mthod return the...
28
by: Marc Gravell | last post by:
In Linq, you can apparently get a meaningful body from and expression's .ToString(); random question - does anybody know if linq also includes a parser? It just seemed it might be a handy way to...
18
by: Just Another Victim of the Ambient Morality | last post by:
Is pyparsing really a recursive descent parser? I ask this because there are grammars it can't parse that my recursive descent parser would parse, should I have written one. For instance: ...
4
by: manu | last post by:
Hi, I need to parse xml files for the Blender Game Engine. ATM I am trying to get this script running in the BGE. This is my first script and I dont have much experience programming... import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.