|
 |
|
|
07-31-2006, 02:19 AM
|
Help me with XML feeds
|
Posts: 30
|
Is there anyone in here who knows about xml feeds? Anyone who knows a good deal of sources to read or something that can give out a sample written code?
Thanks guys.
|
|
|
|
07-31-2006, 05:56 PM
|
|
Posts: 10
|
I am also needy of XML tuts. I've found many through Google but they explain how to write hundreds of pages just to read an (unrelated) XML document
|
|
|
|
07-31-2006, 07:09 PM
|
|
Posts: 1,534
|
Hate to be a third, but I've always been not so good with this... I'm interested in a little guidance too.
__________________
AdminAddict.com - Please login or register to view this content. Registration is FREE
|
|
|
|
07-31-2006, 09:41 PM
|
|
Posts: 224
|
Try this code:
Code:
<?php
function GetXMLTree ($xmldata)
{
// we want to know if an error occurs
ini_set ('track_errors', '1');
$xmlreaderror = false;
$parser = xml_parser_create ('ISO-8859-1');
xml_parser_set_option ($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parser_set_option ($parser, XML_OPTION_CASE_FOLDING, 0);
if (!xml_parse_into_struct ($parser, $xmldata, $vals, $index)) {
$xmlreaderror = true;
echo "error";
}
xml_parser_free ($parser);
if (!$xmlreaderror) {
$result = array ();
$i = 0;
if (isset ($vals [$i]['attributes']))
foreach (array_keys ($vals [$i]['attributes']) as $attkey)
$attributes [$attkey] = $vals [$i]['attributes'][$attkey];
$result [$vals [$i]['tag']] = array_merge ($attributes, GetChildren ($vals, $i, 'open'));
}
ini_set ('track_errors', '0');
return $result;
}
function GetChildren ($vals, &$i, $type)
{
if ($type == 'complete') {
if (isset ($vals [$i]['value']))
return ($vals [$i]['value']);
else
return '';
}
$children = array (); // Contains node data
/* Loop through children */
while ($vals [++$i]['type'] != 'close') {
$type = $vals [$i]['type'];
// first check if we already have one and need to create an array
if (isset ($children [$vals [$i]['tag']])) {
if (is_array ($children [$vals [$i]['tag']])) {
$temp = array_keys ($children [$vals [$i]['tag']]);
// there is one of these things already and it is itself an array
if (is_string ($temp [0])) {
$a = $children [$vals [$i]['tag']];
unset ($children [$vals [$i]['tag']]);
$children [$vals [$i]['tag']][0] = $a;
}
} else {
$a = $children [$vals [$i]['tag']];
unset ($children [$vals [$i]['tag']]);
$children [$vals [$i]['tag']][0] = $a;
}
$children [$vals [$i]['tag']][] = GetChildren ($vals, $i, $type);
} else
$children [$vals [$i]['tag']] = GetChildren ($vals, $i, $type);
// I don't think I need attributes but this is how I would do them:
if (isset ($vals [$i]['attributes'])) {
$attributes = array ();
foreach (array_keys ($vals [$i]['attributes']) as $attkey)
$attributes [$attkey] = $vals [$i]['attributes'][$attkey];
// now check: do we already have an array or a value?
if (isset ($children [$vals [$i]['tag']])) {
// case where there is an attribute but no value, a complete with an attribute in other words
if ($children [$vals [$i]['tag']] == '') {
unset ($children [$vals [$i]['tag']]);
$children [$vals [$i]['tag']] = $attributes;
}
// case where there is an array of identical items with attributes
elseif (is_array ($children [$vals [$i]['tag']])) {
$index = count ($children [$vals [$i]['tag']]) - 1;
// probably also have to check here whether the individual item is also an array or not or what... all a bit messy
if ($children [$vals [$i]['tag']][$index] == '') {
unset ($children [$vals [$i]['tag']][$index]);
$children [$vals [$i]['tag']][$index] = $attributes;
}
$children [$vals [$i]['tag']][$index] = array_merge ($children [$vals [$i]['tag']][$index], $attributes);
} else {
$value = $children [$vals [$i]['tag']];
unset ($children [$vals [$i]['tag']]);
$children [$vals [$i]['tag']]['value'] = $value;
$children [$vals [$i]['tag']] = array_merge ($children [$vals [$i]['tag']], $attributes);
}
} else
$children [$vals [$i]['tag']] = $attributes;
}
}
return $children;
}
$url = "http://www.yourdomain.com/test.xml"; //URL of the XML FEED
$contents = file_get_contents($url);
$data = GetXMLTree ($contents);
print_r($data);
?>
Toward the bottom of this you can just put in your URL that you are trying to parse/read.
That will get the XML into a PHP array that you can then do with what you want.
If you are not a PHP guy, you will need to comment out the print_r($data); line as well once you see that it works.
Source: http://www.zend.com/codex.php?id=1455&single=1
|
|
|
|
08-01-2006, 02:29 AM
|
|
Posts: 406
|
what exactly are you trying to do with the xml? that's the important part.
|
|
|
|
08-01-2006, 04:30 AM
|
|
Posts: 30
|
To Briansol:
I'll be working with someone who will install some SEs into an interface that would generate us some data. After installation, I will be assigned to write xml to combine the data and present them to previous interface. Unfortunately, this will be my firs time to work on xml feeds. A few exmaples as to how it looks like and how it functions would be very helpful.
To dadofgage:
I'll be keeping the sample codes and see where can I specifically include that in my work. Thanks! Great tip! Hope it will work.
By the way, do all programmers in this universe know all about xml feeds?
|
|
|
|
08-01-2006, 08:08 AM
|
|
Posts: 224
|
Quote:
|
By the way, do all programmers in this universe know all about xml feeds?
|
Pretty much, yes.
|
|
|
|
08-08-2006, 04:26 AM
|
|
Posts: 30
|
Hello dadofage!
I used the codes you have provided. So far I haven't received any feedback yet but I am hoping that it's the right code to get what what my client really wanted me to work on.
Regarding my question, all of my programmer friends gave me RSS thingy for this query. Am I right in saying that xml feeds are written as RSS feeds?
|
|
|
|
08-08-2006, 12:32 PM
|
|
Posts: 504
Name: Nick Ohrn
|
Quote:
Originally Posted by epithet
Hello dadofage!
I used the codes you have provided. So far I haven't received any feedback yet but I am hoping that it's the right code to get what what my client really wanted me to work on.
Regarding my question, all of my programmer friends gave me RSS thingy for this query. Am I right in saying that xml feeds are written as RSS feeds?
|
I may be incorrect, but I believe that RSS feeds are just a form of XML. XML is very, very versatile, because you can tag content as anything you want, formulate your own structure,and produce a reader to analyze it. RSS, I believe, is just a standard type of XML file that is produced. Again, however, I could be wrong.
-Nick
__________________
Please login or register to view this content. Registration is FREE - Custom plugin development to fit your needs. Plugins available for WordPress and Drupal, among others.
|
|
|
|
08-08-2006, 11:11 PM
|
|
Posts: 20
|
Quote:
Originally Posted by nickohrn
I may be incorrect, but I believe that RSS feeds are just a form of XML. XML is very, very versatile, because you can tag content as anything you want, formulate your own structure,and produce a reader to analyze it. RSS, I believe, is just a standard type of XML file that is produced. Again, however, I could be wrong.
|
That's correct. RSS is a standard method of using XML for a feed.
Much like HTML (technically XHTML) is a standard method of interface design with XML.
I think really the only tough thing when it comes to coding scripts/apps that mess with XML is you need to have a proper understanding of parent/child/root nodes. If you get that and have good general coding knowledge (particularly loops), you should find it relatively easy coding.
|
|
|
|
08-17-2006, 04:37 AM
|
|
Posts: 30
|
It's the other way. An rss feed is basically an xml document that is created in accordance with the rss specification.
If you fancy a read this is probably the best source of info i have found on rss specs blogs dot law dot harvard dot edu slash tech slash rss.
|
|
|
|
|
« Reply to Help me with XML feeds
|
|
|
| Thread Tools |
Search 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
HTML code is Off
|
|
|
|