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]
12 1958
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?
If you have PHP5 (which you should!), I'd suggest that you use the DOM. It's much easier and follows the W3C standards.
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
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;
-
}
-
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
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;
-
}
-
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 ............
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.
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
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);
-
Ya man
it works Great
thank you
im sorry for big thread
Heya, Abdoelmasry.
That's what we're here for ~_^
Sign in to post your reply or Sign up for a free account.
Similar topics
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)...
|
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...
|
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...
|
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...
|
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 +
...
|
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...
|
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...
|
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:
...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
| |