Go Back   CodingForums.com > :: Client side development > XML

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-30-2008, 08:09 AM   PM User | #1
dustywb
New Coder

 
Join Date: Nov 2004
Posts: 55
Thanks: 8
Thanked 2 Times in 1 Post
dustywb is an unknown quantity at this point
Displaying XML Data on a web page

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
dustywb is offline   Reply With Quote
Old 10-30-2008, 09:22 AM   PM User | #2
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
Quote:
Originally Posted by dustywb View Post
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
oesxyl is offline   Reply With Quote
Old 10-30-2008, 10:14 AM   PM User | #3
dustywb
New Coder

 
Join Date: Nov 2004
Posts: 55
Thanks: 8
Thanked 2 Times in 1 Post
dustywb is an unknown quantity at this point
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
dustywb is offline   Reply With Quote
Old 10-30-2008, 04:23 PM   PM User | #4
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
Quote:
Originally Posted by dustywb View Post
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
oesxyl is offline   Reply With Quote
Old 10-30-2008, 06:03 PM   PM User | #5
dustywb
New Coder

 
Join Date: Nov 2004
Posts: 55
Thanks: 8
Thanked 2 Times in 1 Post
dustywb is an unknown quantity at this point
I'm trying to figure it out, looks a bit confusing, anyways here's my code
PHP 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>';
      }
    }

dustywb is offline   Reply With Quote
Old 10-31-2008, 09:08 AM   PM User | #6
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
try this:
PHP Code:
<?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 is offline   Reply With Quote
The Following 2 Users Say Thank You to oesxyl For This Useful Post:
dustywb (10-31-2008), itsallkizza (10-31-2008)
Old 10-31-2008, 03:56 PM   PM User | #7
itsallkizza
Senior Coder

 
Join Date: Oct 2008
Location: Long Beach
Posts: 1,196
Thanks: 36
Thanked 164 Times in 164 Posts
itsallkizza will become famous soon enough
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
itsallkizza is offline   Reply With Quote
Old 10-31-2008, 06:37 PM   PM User | #8
dustywb
New Coder

 
Join Date: Nov 2004
Posts: 55
Thanks: 8
Thanked 2 Times in 1 Post
dustywb is an unknown quantity at this point
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 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>


Quote:
Originally Posted by oesxyl View Post
try this:
PHP Code:
<?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
dustywb is offline   Reply With Quote
Old 11-01-2008, 12:47 AM   PM User | #9
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
Quote:
Originally Posted by dustywb View Post
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
it's no problem,
to extract field name with value "Some Guy", xpath expression is /info/name
Code:
<?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 Code:
<?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>
'
;
    }
  }
}

Last edited by oesxyl; 11-01-2008 at 12:49 AM..
oesxyl is offline   Reply With Quote
Users who have thanked oesxyl for this post:
dustywb (11-01-2008)
Old 11-01-2008, 04:06 AM   PM User | #10
dustywb
New Coder

 
Join Date: Nov 2004
Posts: 55
Thanks: 8
Thanked 2 Times in 1 Post
dustywb is an unknown quantity at this point
Smile

WOOHOO OMFG IT WORKS YOU ARE AWESOME I BOW BEFORE YOU... Lol but seriously thanks a ton, it works great!


Quote:
Originally Posted by oesxyl View Post
it's no problem,
to extract field name with value "Some Guy", xpath expression is /info/name
Code:
<?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 Code:
<?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 is offline   Reply With Quote
Old 11-01-2008, 04:16 AM   PM User | #11
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
Quote:
Originally Posted by dustywb View Post
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
oesxyl is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:23 AM.


Advertisement
Log in to turn off these ads.