View Full Version : Inserting external .txt file into an XML element
PowerDrift
10-04-2005, 07:19 AM
Is it possible?
I really would like to do this.
By doing this, I can create a PHP script to edit the flat .txt file, which in return is displayed in the XML file. The file will then be dynamic, and the XML file will not have to be changed manually, but instead with a PHP content manager.
Fou-Lu
10-04-2005, 05:06 PM
This is really more of a php question, as generating it from textfile to XML requires a method of somesorts, and since you've mentioned PHP we'll go with that.
The answer is yes. But it does depend on what you have, what you want to have/do, and what methods you have available to you. I'd suggest dropping your textfiles completely once you migrate to ths use of XML, as PHP can generate complete XML files on its own, as well as process them.
However, to do this you need to have either php5.x with integrated CORE XML, or you can use php 4.x with libxml and I believe sablot (+iconv) libraries configured. With these methods you can then manipulate the XML using the DOM and DOMXML functions (depending on php version).
If you would like a PHP method for this, I can create an example for you if you can provide me with your php version, an example textfile, and an example xml file of what the textfile should look like.
Also, if you decide on a php based route, could a moderator move this thread (mainly so I don't forget about it :p)
PowerDrift
10-05-2005, 01:32 AM
Actually, I was trying to stay away from the PHP replacing the XML. But if its the only way, I really dont have a choice.
Now, what to you mean by an example of a PHP file?
Fou-Lu
10-05-2005, 06:03 AM
Not replacing XML, PHP co-existing with XML.
See, now I may be confused, my understanding is that you want to import a textfile into an XML file to be processed by php. What I'm suggesting is why bother do that when there are easier ways, such as completely replacing the textfile itself with a variable driven XML file which is processed by the PHP instead. This would be useful with for say a template based system, especially if you want to process XSL server side for markup. XML is a great match for php especially if SQL isn't available for your use. Am I mistaken for what it is you are seeking?
PowerDrift
10-05-2005, 06:26 AM
Yes, I would like to use a template with the external textfile(s) to be inserted for content. I also do not want to use XSL (if I had, i wouldnt be having this problem). Right now I have the stucture of the presentation using XML and CSS. It can be seen here (http://khs.studiopd.com).
So basically what you are saying is to parse the XML with PHP and then use the power of PHP for inserting the data into the template?
Fou-Lu
10-05-2005, 05:03 PM
Yes, XML can be used this way. I see what you are doing here as well, (I like it btw, nice and simple, professional in look, good color coordination etc), but I do have a couple things to suggest with this alone. XML cannot be processed using all browsers. With this in mind, I'd suggest that output be presented as XHTML (w/ incorrect text/html mime) instead. I'd also suggest using a xsl stylesheet instead (though I'm thouroughly satisfied by your CSS display), as XSL combined with XML can be processed for XHTML output using PHP. This will allow many more browsers to access this data.
With this aside, you can create XML with php variables within:
<?xml version="1.0"?>
<root>
<title>$title</title>
<whatever>More Stuff</title>
</root>
That $title can be accessed using DOM or DOMXML (again, I need your version to define an example for you), but once you gather the value of node title, you can use an eval() statement to assign it. Its kind of hard to explain without showing you. This method works perfectly with templates, as you can also assign multiple templates into the XML file itself:
<?xml version="1.0"?>
<templates>
<template name="index_shell">
<!-- Data for index shell -->
</template>
<template name="whatever">
<!-- Etc -->
</template>
</templates>
for example.
I would like to suggest however, if SQL is available for your use, templates are a lot easier to use database driven than using XML, combined with XHTML 1.0 compliancy. Well, more accuratly, its easier to find knowledgable people with the SQL incorperation than it is for the XML in case you are having any troubles with it.
Can you give me your php version so I can create a simple example for you?
PowerDrift
10-05-2005, 06:05 PM
I am using PHP version 4.3.
So with the variables in the XML file, the PHP will parse the XML, which is when the variables get replaced?
Fou-Lu
10-06-2005, 04:16 PM
Not directly, you will most probably need to use an eval() for it to work. I will however test it out to confirm this.
I'll post back later with a quick example for you as well. I will combine multiple templates into one XML file, and throw together a quick example.
PowerDrift
10-06-2005, 06:53 PM
Okay.
Thank you very much :)
Fou-Lu
10-09-2005, 11:38 AM
Hey powerdrift
Sorry for the dely, my home network is well... fubar'd ATM, so it will probably be a couple of days before I can get it back online (currently at work :p). I'll get you an example as soon as possible.
PowerDrift
10-09-2005, 10:20 PM
No problem. Im just glad youre helping me :rolleyes:
Fou-Lu
10-14-2005, 01:58 PM
Okay, so I've gotten myself stable enough to start on this for you.
I'm just looking at your source here, and I'm wondering where you would be combining your php into (i see many places for it), so I'm not certain quite how you want your output. I'm assuming as well you would still like the output to be in XML as well, so I'll take that into account also.
What I do know sofar is two important things:
1. Php requires you to evaluate the code as php.
2. You need php's DOMXML API configured. If your on server, chance are they are already configured.
Now, if you could clarify your templates for me as well that would be great. I'm looking at your index.xml page, and will consider the works as the shell.
So, my template shell would be similar to so:
<template id="page_shell">
<data><![CDATA[<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/css" href="style.css"?>
<!DOCTYPE totalcontainer SYSTEM "validator.dtd">
$body
</template>
With each set inside as a template of their own:
<template id="subcontainer">
<prefix>xlink</prefix>
<namespace-uri><![CDATA[http://www.w3.org/1999/xlink]]></namespace-uri>
<type>simple</type>
<href>$url</href>
<display>$display</display>
</template>
<template id="link">
<hover>$hover_char</hover>
<text>$text</text>
<subcontainer>$subcontainer</subcontainer>
</template>
For instance (I just threw this together off the top of my head).
Now, we will use php to create each set from whatever source you would like, and create variables based on each value.
We'll pretend we have an array here, gathered from SQL or text data. The $link_array would become massive in its dimensions, but not beyond alterations. This example we create it in, opposed to in practice where we would dynamically generate it:
$link_array = array(
array('href' => 'page.php', 'display' => 'Home Page'),
array('href' => 'page2.php', 'display' => 'Search'),
array('href' => 'contact.php', 'display' => 'Contact')
);
// Here, we would do a match for each set of [link]. However, we do not have the ability to do this with this example
$hover_char = '*';
$text = 'This is one link (we cannot generate more than one.)';
foreach ($link_array AS $each_links_array)
{
eval("\$subcontainer .= \"" . parse_template('subcontainer', $each_links_array) . "\";");
}
// Etc.
This is just an idea sort of method. What the function parse_template will do is create and return the xml required to construct the page for the subcontainers. So, the output in this example would be:
<sublink xlink:type="simple" xlink:href="page.php">Home Page</sublink>
<sublink xlink:type="simple" xlink:href="page2.php">Search</sublink>
<sublink xlink:type="simple" xlink:href="contact.php">Contact</sublink>
The prefix for the xlink namespace would be added to the page shell instead.
Before I continue and actually make up one that works, I thought I would check once more. This is the idea that you are looking at needing correct?
PowerDrift
10-14-2005, 06:21 PM
Wow! Thats exactly what I was looking for :D
Please continue. Thanks again
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.