You are here:  » Dynamic XML Site


Dynamic XML Site

Submitted by dustin on Mon, 2006-03-06 19:16 in

I am using XML as my database and I want to be able to use it in a dynamic page set-up.

page.php?id=id

<examples>
<example id=id1 name=asdad info1=asd />
<example id=id2 name=asdad info1=asd />
<example id=id3 name=asdad info1=asd />
<example id=id4 name=asdad info1=asd />
</examples>

XML is set-up like that.

If you type in page.php?id=id1 then I want to be able to pull the data (name and info1) from that line.

Hello my name is {name from xml}, The info for today is {info1 from XML}

Just examples above, but what I am trying to do is simple I just don't know where to start.

Thanks

Submitted by support on Mon, 2006-03-06 20:34

Hi -

Just tidied up the post using the code tags and working on some demo code for this now - check back soon...!

David.

Submitted by dustin on Mon, 2006-03-06 21:23

Great! Thanks...this is going to save me alot of time.

I purchased a script that uses XML in this way and I tried to customize it for another site of mine, but it has so many classes and functions for other aspects of the site when I start removing stuff it just fills up with errors.

---

Also do you think using XML for a database instead of MYSql will make the site load faster? (just a side question) I am sure it will, but I want to see what you have to say.

Thanks again

Submitted by support on Mon, 2006-03-06 21:39

Hello dustin,

With regards to whether using XML instead of a database will speed things up for your site i'm afraid the general answer is no - if there is a lot of information to search through.

This is because databases create an index that enables them to find the required record (in your case where id=???) very quickly; whereas with XML you have no choice but to scan through the entire file until you find the record you want.

In a very simple example as per your original post; an XML solution would be very quick; but as soon as you start to increase the number of lines of XML that have to be read you will notice a signficant decrease in performance over a database solution.

To take you through how you would use Magic Parser to do this; you would start with a file containing your XML in the same directory as your script...

info.xml:

<examples>
<example id=id1 name=asdad info1=asd />
<example id=id2 name=asdad info1=asd />
<example id=id3 name=asdad info1=asd />
<example id=id4 name=asdad info1=asd />
</examples>

Then, your script would look like this (see comments for how it works)...

page.php:

<?php
  
require("MagicParser.php");
  
// get the id from the query string ?id=
  
$id $_GET["id"]
  function 
myRecordHandler($record)
  {
    global 
$id;
    global 
$name;
    global 
$info1;
    
// look to see if id matches the one we want:
    
if ($id == $record["EXAMPLE-ID"])
    {
      
// set the global variables if they do
      
$name $record["EXAMPLE-NAME"];
      
$info1 $record["EXAMPLE-INFO1"];
      
// don't read any more records, we found the one we want
      
return true;
    }
  }
  
MagicParser_parse("info.xml","myRecordHandler","xml|EXAMPLES/EXAMPLE/")
  if (
$name)
  {
    print 
"<p>Hello my name is ".$name.", The info for today is ".$info1."</p>";
  }
  else
  {
    print 
"<p>Sorry, ID not found.</p>";
  }
?>

Hope this makes sense!

Regards,
David.

Submitted by dustin on Mon, 2006-03-06 23:36

Can I echo the $name and $info1?

php echo $name

Just include or require this on my index.php page?

Submitted by dustin on Tue, 2006-03-07 12:31

What if I set-up individual .xml files for each page I am creating?

For example.

I will have main.xml and it will have id="" and xml-file=""

So the main.xml file will just have the ids and the path to the xml file for the page. Is it possiable to parse files like that? Would that help speed at all?

I know I will have lots of XML files, but the way I am settting up the site XML is going to be used anyways.

Submitted by support on Tue, 2006-03-07 13:14

Hi,

Yes - that is a good idea. Your first, smaller XML file then effectively becomes your "index", and the second one is where all the data for that page is.... So you're effectively doing what a database does when creating an index...!

Regarding your previous message - yes, you can simply "echo $name" - once you've extracted $name from your XML you can use it just like any other PHP variable.

Submitted by dustin on Tue, 2006-03-07 13:26

Okay cool

I am have trouble with the script above...it keeps saying id not found :(

Submitted by dustin on Tue, 2006-03-07 13:28

<?php
  require("MagicParser.php");
  // get the id from the query string ?id=
  $id = $_GET["id"];
  function myRecordHandler($record)
  {
    global $id;
    global $name;
    global $info1;
    // look to see if id matches the one we want:
    if ($id == $record["EXAMPLE-ID"])
    {
      // set the global variables if they do
      $name = $record["EXAMPLE-NAME"];
      $info1 = $record["EXAMPLE-INFO1"];
      // don't read any more records, we found the one we want
      return true;
    }
  }
  MagicParser_parse("examples.xml","myRecordHandler","xml|EXAMPLES/EXAMPLE/");
  if ($name)
  {
    print "<p>Hello my name is ".$name.", The info for today is ".$info1."</p>";
  }
  else
  {
    print "<p>Sorry, ID not found.</p>";
  }
?>

That has some ; missing...that is a fixed version, but like I said in my last message it doesn't work.

Are you sure EXAMPLE-ID is correct way to do look for ID?

Submitted by support on Tue, 2006-03-07 13:31

Hi,

Yes - that's right; but i've just noticed that the XML is not valid. The attributes must be quoted, so try this instead in your info.xml:

<examples>
<example id='id1' name='asdad' info1='asd' />
<example id='id2' name='asdad' info1='asd' />
<example id='id3' name='asdad' info1='asd' />
<example id='id4' name='asdad' info1='asd' />
</examples>

Here is the above XML pasted into the demo script:

http://www.magicparser.com/demo?fileID=440D8B20B5C37&record=1

Submitted by dustin on Tue, 2006-03-07 13:36

That works now.

Are you still suggesting using the array?

Submitted by dustin on Tue, 2006-03-07 13:38

Also I should be able to make info1 the path to another xml file and the use the code from another thread to parse two xml files just set the path in the parser to $info1.

This stuff is starting to make sense! I haven't done any programming in about 2 years.

Submitted by support on Tue, 2006-03-07 13:52

Yes - sounds like you're on the right tracks.

Remember that your record handler doesn't have to be called myRecordHandler, so it will make things easier if you're going to be parsing multiple XML documents in a single script to use different names. Study this example to see what I mean - this is not complete just something to show you basically what we're talking about:

<?php
  
require("MagicParser.php");
  
// get the id from the query string ?id=
  
$id $_GET["id"];
  function 
myRecordHandlerIndex($record)
  {
    global 
$id;
    global 
$name;
    global 
$info1;
    
// look to see if id matches the one we want:
    
if ($id == $record["EXAMPLE-ID"])
    {
      
// set the global variables if they do
      
$name $record["EXAMPLE-NAME"];
      
$info1 $record["EXAMPLE-INFO1"];
      
// don't read any more records, we found the one we want
      
return true;
    }
  }
  
MagicParser_parse("examples.xml","myRecordHandlerIndex","xml|EXAMPLES/EXAMPLE/");
  if (!
$name)
  {
    print 
"<p>Sorry, ID not found.</p>";
    exit();
  }
  function 
myRecordHandlerInfo($record)
  {
    
// use information from the info1 XML document
  
}
  
MagicParser_parse($info1,"myRecordHandlerInfo","xml|FOO/BAR/");
?>

Of course "xml|FOO/BAR/" is just an example format string for your second XML file containing the info. Remember that you can experiment with the demo script on this website to work out the correct format string for your info XML.

Submitted by dustin on Tue, 2006-03-07 14:03

it wont let me post says terminated b/c of suspecious input??

Submitted by dustin on Tue, 2006-03-07 14:05

I'm testing out the php echos. I have an index file (wont let me post the code to it) (index.php). It has an include for parser.php (the file below) and two echos.

<?php echo($name); ?>

Parser.php is a moded version of the file you made for me...

<?php
  require("MagicParser.php");
  // get the id from the query string ?id=
  $id = $_GET["id"];
  function myRecordHandler($record)
  {
    global $id;
    global $name;
    global $info1;
    // look to see if id matches the one we want:
    if ($id == $record["EXAMPLE-ID"])
    {
      // set the global variables if they do
      $name = $record["EXAMPLE-NAME"];
      $info1 = $record["EXAMPLE-INFO1"];
      // don't read any more records, we found the one we want
      return true;
    }
  }
  MagicParser_parse("examples.xml","myRecordHandler","xml|EXAMPLES/EXAMPLE/");
?>

I have a feeling I am leaving something out or something...

Submitted by support on Tue, 2006-03-07 14:20

Hi,

Is the problem that when you echo($name) there is nothing displayed...

One thing that will help you debug (I mentioned this in the other thread) is the print_r() function. If you add the following 2 lines at the top of myRecordHandler() you will be able to study what $record contains for the XML and format string you are using:

  function myRecordHandler($record)
  {
    print_r($record);
    exit();
    ... rest of script ...

This will display the contents of $record, then exit() immediately...

Submitted by dustin on Tue, 2006-03-07 14:24

yes the index.php page is blank.

Submitted by dustin on Tue, 2006-03-07 14:30

got that working...it was an id10t error on my end.

renames examples.xml for testing and forgot to change it back on my web server...

Submitted by dustin on Tue, 2006-03-07 14:36

Also want to run this by you.

I am planning to set-up 4 sets of xml files for one page.

I set-up a directory called /xml/

index.xml is going to have all the ids. (/xml/index.xml)

<businesses>
<example id='business-name' />
<example id='business-name' />
<example id='business-name' />
<example id='business-name' />
<example id='business-name' />
<example id='business-name' />
<example id='business-name' />
<example id='business-name' />
<example id='business-name' />
<example id='business-name' />
<example id='business-name' />
<example id='business-name' />
<example id='business-name' />
<example id='business-name' />
<example id='business-name' />
<example id='business-name' />
<example id='business-name' />
<example id='business-name' />
<example id='business-name' />
<example id='business-name' />
<example id='business-name' />
<example id='business-name' />
<example id='business-name' />
<example id='business-name' />
<example id='business-name' />
<example id='business-name' />
<example id='business-name' />
<example id='business-name' />
</businesses>

The I am going to have three folders set-up for different info that will be pulled for each different id.

/xml/businesses (have all the general info like address, etc.)
/xml/events (Will have events example)
/xml/maps (google maps api...will have the coordinates)

inside each folder will be an xml file that has the same name as "id" so business-name.xml will be in each folder.

In the parser.php file I will call '/folder_name/$id.xml' that way the largest file will only have to have ids in it and not different xml file paths...I think this will be easier too.

You think this will work?

Submitted by support on Tue, 2006-03-07 14:40

Yes - that sounds straight forward - I think you meant that in each directory you will have $id.xml rather than business-name.xml, for example:

/xml/businesses/1.xml
/xml/businesses/2.xml

/xml/events/1.xml

Sounds good to me...!

Submitted by dustin on Tue, 2006-03-07 14:42

well the ids will be names.

I am setting up mod_rewrite so my urls will be SEO friendly.

so mysite.com/business-name

instead of mysite.com/index.php?id=1

Submitted by dustin on Tue, 2006-03-07 15:04

Any idea why this won't work?

$burl = base_url;
  MagicParser_parse(".$burl.""index.xml","indexRecordHandler","xml|BUSINESSES/BUSINESS/");

Submitted by support on Tue, 2006-03-07 15:18

The dot operator to concatenate strings isn't valid within a string itself. Try this instead:

$burl = base_url;
MagicParser_parse($burl."index.xml","indexRecordHandler","xml|BUSINESSES/BUSINESS/");

Submitted by dustin on Tue, 2006-03-07 15:21

that seems to work.

Submitted by dustin on Tue, 2006-03-07 15:37

How would I format something in this situation?

MagicParser_parse($burl."businesses/".$id.xml","myRecordHandlerInfo","xml|FOO/BAR/");

base_url/business/$id.xml

Submitted by dustin on Tue, 2006-03-07 15:43

This seems to work

MagicParser_parse($burl."businesses/".$id.".xml","myRecordHandlerInfo","xml|FOO/BAR/");

Submitted by dustin on Tue, 2006-03-07 16:49

Just an update

I got it to pull from all 4 XML files and so far everything is working great. I had to change around lots of stuff to get everything to work but it is turning out good!

I did have to change id to gid...so it is ?gid=id

For some reason if I just have id it don't work correctly and allows anything to be assigned as id. But it works perfect using gid.

Submitted by crounauer on Fri, 2006-03-31 12:47

Hi David,

Is it possible to use an XML tag i.e."&item["HOTEL_CITY"]" which has previously been used in an XML query higher up in a page for another XML feed.?

$url4 = "http://xxxxxhp3?pid=1000&ctry=1&city=".&item["HOTEL_CITY"]."";

Think I need to declare &item["HOTEL_CITY"] global?

Submitted by support on Fri, 2006-03-31 12:55

Yes - just use a global variable $hotel_city; and then set it wherever you want, then you can use it anywhere else in the script (provided that you've global'ed it in of course...)

For example:

<?php
  $hotel_city 
"":
  function 
myRecordHandler($item)
  {
    global 
$hotel_city;
    
$hotel_city $item["HOTEL_CITY"];
    
// process as normal
  
}
  
MagicParser_parse("file.xml","myRecordHandler");
  function 
anotherRecordHandler($item)
  {
    global 
$hotel_city;
    
// use $hotel_city here
  
}
  
MagicParser_parse("anotherfile.xml","anotherRecordHandler");
?>

Hope this helps!