...

View Full Version : Displaying XML Data on a web page



dustywb
10-30-2008, 09:09 AM
Hi, ok I'm going to start from the beginning. I have a php script that lists all of the xml files in a directory by filename. I am wondering if there is a way instead of it listing by filename to get a fields data in the xml file and display it by that. so instead of getting filename.xml it would get for example <field>Title Name</field> and display Title Name. Hope this question makes sense lol. Thanks in advance

oesxyl
10-30-2008, 10:22 AM
Hi, ok I'm going to start from the beginning. I have a php script that lists all of the xml files in a directory by filename. I am wondering if there is a way instead of it listing by filename to get a fields data in the xml file and display it by that. so instead of getting filename.xml it would get for example <field>Title Name</field> and display Title Name. Hope this question makes sense lol. Thanks in advance
'Title Name' field is in a separate xml or exists in each xml file from that directory?
You have multiple solution in both cases but you need to build or get the pair filename - title name someway

best regards

dustywb
10-30-2008, 11:14 AM
Thanks for the reply. There are multiple xml files in the directory, not a specific amount it's sort of a contact directory, anyone can add their information to it so theres no specific amount of people. Each xml file has a name field and I would rather just so it looks pretty have the link name be the persons name that is specified in the xml file instead of the name of the xml file. For example instead of "thisfile.xml" would have "Juan Johnson" or whatever

oesxyl
10-30-2008, 05:23 PM
Thanks for the reply. There are multiple xml files in the directory, not a specific amount it's sort of a contact directory, anyone can add their information to it so theres no specific amount of people. Each xml file has a name field and I would rather just so it looks pretty have the link name be the persons name that is specified in the xml file instead of the name of the xml file. For example instead of "thisfile.xml" would have "Juan Johnson" or whatever
you can use dom or simplexml php extensions:

http://www.php.net/manual/en/book.dom.php
http://www.php.net/manual/en/book.simplexml.php

based on what you have done until now, you can use the file list from your script to open each xml file, read the field and build a hash array with keys filename and values the value of the field for each filename.
if you post the code you have I guess I could say where and what to modify.

best regards

dustywb
10-30-2008, 07:03 PM
I'm trying to figure it out, looks a bit confusing, anyways here's my code


$ext = 'xml';
$dirname = "pages/";
$files = scandir($dirname);

foreach($files as $file)
{
if(($file != ".") and ($file != ".."))
{
$fileChunks = explode(".", $file);
if($fileChunks[1] == $ext) {

echo '<div class="info"><a href="#" onclick=\'open("pages/' . $file . '","","width=300,height=250"); return false;\'>' . $file . '</a></div>';
}
}
}

oesxyl
10-31-2008, 10:08 AM
try this:


<?php
$ext = 'xml';
$dirname = "pages/";
$files = scandir($dirname);

foreach($files as $file){
if(($file != ".") and ($file != "..")){
$fileChunks = explode(".", $file);
if($fileChunks[1] == $ext){
$title = $file;
$doc = new DOMDocument();
if($doc && $doc->load($file)){
$xpath = new DOMXPath($doc);
$query = "*//field";
$entries = $xpath->query($query);
if($entries->length > 0){
$title = $entries->item(0)->nodeValue;
}
}

echo '<div class="info"><a href="#" onclick=\'open("pages/' . $file . '","","width=300,height=250"); return false;\'>' . $title . '</a></div>
';
}
}
}

the xpath expresion from $query must point to your field and you must change it.
post a xml example if you have problem with that.

best regards

itsallkizza
10-31-2008, 04:56 PM
Just wanted to thank your last post because of the scandir function... I'd never seen that before and it's gonna save me a buttload of coding ;)

dustywb
10-31-2008, 07:37 PM
First off let me appologize I feel like I'm being a pain, but I can't seem to get this to work. You have been a big help so far and I can't thank you enough. Hopefully I will understand this stuff eventually, reading and trying but for now most of it doesn't make sense :confused: Anyways heres what the xml files look like:

<info>
<name>Some Guy</name>
<phone>555-555-5555</phone>
<address>1313 Elm St</address>
<city>Sometown</city>
<state>OK</state>
<zip>77777</zip>
<email>email@mail.net</email>
<website>www.codingforums.com</website>
</info>



try this:


<?php
$ext = 'xml';
$dirname = "pages/";
$files = scandir($dirname);

foreach($files as $file){
if(($file != ".") and ($file != "..")){
$fileChunks = explode(".", $file);
if($fileChunks[1] == $ext){
$title = $file;
$doc = new DOMDocument();
if($doc && $doc->load($file)){
$xpath = new DOMXPath($doc);
$query = "*//field";
$entries = $xpath->query($query);
if($entries->length > 0){
$title = $entries->item(0)->nodeValue;
}
}

echo '<div class="info"><a href="#" onclick=\'open("pages/' . $file . '","","width=300,height=250"); return false;\'>' . $title . '</a></div>
';
}
}
}

the xpath expresion from $query must point to your field and you must change it.
post a xml example if you have problem with that.

best regards

oesxyl
11-01-2008, 01:47 AM
First off let me appologize I feel like I'm being a pain, but I can't seem to get this to work. You have been a big help so far and I can't thank you enough. Hopefully I will understand this stuff eventually, reading and trying but for now most of it doesn't make sense :confused:
it's no problem, :)
to extract field name with value "Some Guy", xpath expression is /info/name


<?xml version="1.0"?>
<info>
<name>Some Guy</name>
<phone>555-555-5555</phone>
<address>1313 Elm St</address>
<city>Sometown</city>
<state>OK</state>
<zip>77777</zip>
<email>email@mail.net</email>
<website>www.codingforums.com</website>
</info>




<?php
$ext = 'xml';
$dirname = "pages/";
$files = scandir($dirname);

foreach($files as $file){
if(($file != ".") and ($file != "..")){
$fileChunks = explode(".", $file);
if($fileChunks[1] == $ext){
$title = $file;
$doc = new DOMDocument();
if($doc && $doc->load($file)){
$xpath = new DOMXPath($doc);
$query = "/info/name";
$entries = $xpath->query($query);
if($entries->length > 0){
$title = $entries->item(0)->nodeValue;
}
}

echo '<div class="info"><a href="#" onclick=\'open("pages/' . $file . '","","width=300,height=250"); return false;\'>' . $title . '</a></div>
';
}
}
}

dustywb
11-01-2008, 05:06 AM
WOOHOO OMFG IT WORKS YOU ARE AWESOME I BOW BEFORE YOU... Lol but seriously thanks a ton, it works great!



it's no problem, :)
to extract field name with value "Some Guy", xpath expression is /info/name


<?xml version="1.0"?>
<info>
<name>Some Guy</name>
<phone>555-555-5555</phone>
<address>1313 Elm St</address>
<city>Sometown</city>
<state>OK</state>
<zip>77777</zip>
<email>email@mail.net</email>
<website>www.codingforums.com</website>
</info>




<?php
$ext = 'xml';
$dirname = "pages/";
$files = scandir($dirname);

foreach($files as $file){
if(($file != ".") and ($file != "..")){
$fileChunks = explode(".", $file);
if($fileChunks[1] == $ext){
$title = $file;
$doc = new DOMDocument();
if($doc && $doc->load($file)){
$xpath = new DOMXPath($doc);
$query = "/info/name";
$entries = $xpath->query($query);
if($entries->length > 0){
$title = $entries->item(0)->nodeValue;
}
}

echo '<div class="info"><a href="#" onclick=\'open("pages/' . $file . '","","width=300,height=250"); return false;\'>' . $title . '</a></div>
';
}
}
}

oesxyl
11-01-2008, 05:16 AM
WOOHOO OMFG IT WORKS YOU ARE AWESOME I BOW BEFORE YOU... Lol but seriously thanks a ton, it works great!
you are welcome, :)

best regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum