PHP/XML feed reader where to start? - php

I have a question regarding XML feeds.
There is a website whitch provides data and they give me 2 links for 2 files
file 1 : Cars_docu.atom
file 2 : Cars.atom
date on file 1 is like this
<entry>
<author>
<name />
</author>
<f:name>Name</f:name>
<f:description>Product name</f:description>
and the second file is like this
<entry>
<title>Volvo</title>
<link rel="alternate" href="#1" />
<author>
<name />
</author>
<id>237370</id>
<updated>2015-08-17T11:44:32Z</updated>
<f:name>Volvo Lamp</f:name>
So as I understand the first file includes name of the filds and the second one includes the data
My question is what is the method of pulling this imformation from both files using a PHP file and gather them into one page ?
and inside the second document there is a 10000 product so how i pull one pacific product only?
I need methods if you have code I will be glad or just tell me where to start
regads

Take a look at the SimpleXML Class:
http://php.net/manual/en/class.simplexmlelement.php
The approach to take is to use file_get_contents to pull the data from the XML pages and then use SimpleXML to parse it into an array.
For example:
<?php
$url = "somewebsite.com/Cars.atom";
$file = file_get_contents($url);
$carsArray = new SimpleXMLElement($file);
foreach ($carsArray->entry as $car) {
echo $car->title; // Would display "Volvo"
}
?>
When I'm first investigating the structure of an XML document that has been parsed into an array I find it useful to use the print_r() function.
From the above, the next simple step is to load both XML doc's and work you way through to build the data you want to display.
Edit if you want to match a specific item
Assuming you are wanting to match the car title to Volvo, this is how you could do it.
<?php
$url = "somewebsite.com/Cars.atom";
$file = file_get_contents($url);
$carsArray = new SimpleXMLElement($file);
// Need to use an array key to get the element, alternatively can loop through each value
if($carsArray->entry[0]->title == "Volvo") {
echo "Car is a Volvo!";
}
?>

Related

Display Image from PHP to XML

I am trying to call an image in my PHP to an XML file. I have tried to do this many different ways, but have not had much luck. More specifically, I am trying to call img file variable "Image1" to display. I have also sought tutoring, but the tutor was also somehow stumped on this, so any help is appreciated.
Prompt: (a) Create a XML file which contains the description of at least 3 product names, prices, and image file names.
(b) Load the XML file using Php and generate the shopping items: image, name, price and allow order quantity. Hint: Replace the image file names, item names, and prices with the Php codes. The Php codes supply the Php variables which were filled by reading the XML file at the beginning of the page. Then use "echo" verb to show the content of the session variable which contains the file name, item name, and price. The page should have the file extension .php.
//My XML
<?xml version="1.0" encoding="UTF-8"?>
<items>
<object>
<Name>CSU Womens T-Shirt</Name>
<Material>3% Polyester / 97% Cotton</Material>
<Price>$23.99</Price>
<image1>C:\Users\erica\OneDrive\Documents\IST450HW\IMGs\csupic.jpg</image1>
</object>
<object>
<Name>CSU Unisex Hat</Name>
<Material>2% Spandex / 98% Cotton</Material>
<Price>$10.99</Price>
</object>
<object>
<Name>CSU Men's T-Shirt</Name>
<Material>5% Polyester / 95% Cotton</Material>
<Price>$19.99</Price>
</object>
</items>
//My PHP
<?php
$xml = simplexml_load_file("Items.xml");
foreach ($xml as $key => $value) {
foreach ($value as $key => $value) {
echo $key.": ".$value."</br>";
}
}
$result = $xml->$items->$object->$image1;
echo '<img src="'.$result.'" height="100"; "width="100" ;>';
?>
$xml will be an object, im not sure why you are looping through it with a foreach, so ill ignore that bit.
To access the element which you're after you need to do it like:
$result = $xml->object[0]->image1;
echo '<img src="'.$result.'" height="100" width="100"/>';
But your browser won't see that path (or it wont work once you put it elsewhere), so place the image in an accessible location e.g: ./imgs and then change your xml.
For example:
<image1>./imgs/csupic.jpg</image1>
Then the resulting img tag will look like:
<img src="./imgs/csupic.jpg" height="100" width="100"/>
The web server will then be able to serve the file without issues.
Try put image file csupic.jpg in the same dir of php file and if not work change echo line to:
echo '<img src="/'.$result.'" height="100"; "width="100" ;>';

Make use of an index file for parsing a large XML file

I'm working on a Wikipedia XML Dump file (15GB), which is provided with an index text file.. looking like this :
1628813:431:Bille August
1628813:434:Blues
1628813:435:Bioéthique
1628813:436:Brive-la-Gaillarde
1628813:438:Burdigala
1628813:439:Bouliac
The XML file is basically in this format:
<page>
<title> </title>
<id> </id>
<revision>
<id> </id>
...
<text> </text>
</revision>
</page>
What I want is to get the Text tag content corresponding to specific page Titles.
How can I make use of the index file for quick parsing/searching to achieve this?
P.S: I already tried many solutions for quick parsing and nothing worked for my needs..
I'm working with PHP, I tried SAX parsing, which is quick but didn't allow me to handle the nodes the way I wanted, and tried combining XMLReader and SimpleXML, but it was too slow..

Get information from a form and writing to an xml file using php?

I want to get information from a form and writing to an xml file by using php ? My xml file is in tree structure
Example of my xml file:
<BookStore>
<Book>
<name>TODO</name>
<url>TODO</url>
</Book>
</BookStore>
when I submit it will create a xml but I want Book and BookStore also as a parent. I created till name and url. But I dont know how to create Book and BookStore too?
You just need to create new parent elements and then append the child.
$bookstore = $doc->createElement('Bookstore');
$book = $doc->createElement("Book");
/* your code */
/* append name and url to $book */
$book->appendChild($textareaNode);
$book->appendChild($textareaNode1);
$bookstore->appendChild($book);
$doc->appendChild($bookstore);
$doc->save("d.xml");

SimpleXML and PHP (XPATH)

Ok guys I'm using xml to store page information for a dynamic website. I need to figure out a way to take a variable(file name) and pull the related node and parse that information. so basically here's my XML structure...
<SITE>
<PAGE>
<FILENAME>sub.php</FILENAME>
<DESCRIPTION>this is an example sub page</DESCRIPTION>
<TITLE>NOH Sub page</TITLE>
<PARENTS>
<PARENT>What is NOH</PARENT>
</PARENTS>
</PAGE>
<PAGE>
<FILENAME>about.php</FILENAME>
<DESCRIPTION>description description description </DESCRIPTION>
<TITLE>About Us</TITLE>
<PARENTS>
<PARENT>Company</PARENT>
<PARENT>People</PARENT>
</PARENTS>
</PAGE>
</SITE>
Is there a way to use SimpleXML and PHP to take a variable say.. 'sub.php' and say I want the node where filename = 'sub.php', and then be able to parse that node(page) for needed info. Thanks!
Update, where I'm confused is...
i have this function
function getPage($pagePath){
$file = "includes/sitestructure.xml";
$xml = simplexml_load_file($file);
return print_r($xml-xpath(PAGE/FILENAME));
}
but my xpath doesn't work, and I'm not sure how to logically get the info I need.. I know how to normally parse XML with PHP but not how to specify a specific node by comparing it to a variable then parse it.
$xml-xpath(PAGE/FILENAME)
This is broken in several ways, but this is an XPath question not a PHP syntax one.
You want to get the PAGE element(s) which contain a FILENAME of your chosen file name. To do that in XPath, a predicate can be used.
PAGE[FILENAME="sub.php"]
In your PHP code that might look like
$pages = $xml->xpath('PAGE[FILENAME="sub.php"]');
$first_page = $pages[0];
echo $first_page->TITLE; // NOH Sub page
The important point here is the use of the predicate to filter the result to only the page element(s) that you want.
http://www.w3.org/TR/xpath/#predicates

Fetch URL from XML with jQuery and PHP - Jquery Slideshow

Im using the jQuery Nivo Slider, its pretty simple to setup:
<link rel="stylesheet" href="nivo-slider.css" type="text/css" media="screen" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<script src="jquery.nivo.slider.pack.js" type="text/javascript"></script>
<!-- Then somewhere in the <body> section -->
<div id="slider">
<img src="images/slide1.jpg" alt="" />
<img src="images/slide2.jpg" alt="" title="#htmlcaption" />
<img src="images/slide3.jpg" alt="" title="This is an example of a caption" />
<img src="images/slide4.jpg" alt="" />
</div>
<div id="htmlcaption" class="nivo-html-caption">
<strong>This</strong> is an example of a <em>HTML</em> caption with a link.
</div>
However I want to be able to define the Images and captions in a seperate file, perhaps an XML file or text file. I'd like to store the images in a fairly readable fashion so that virtually anyone could update the file if need be.
So question is how can I generate HTML in the same structure as used above by generated automatically via PHP from a file?
I need to generate a list of images with some options:
I supposse the options are:
IMG src
href
alt
title (The title relates to the caption)
caption (The actual caption text)
Option in the PHP to order the images based on an ID
Only the IMG src, alt and title would be compulsary with the remaining fields optional.
I imagine XML is going to be the easiest way to define these options, but I have no idea how to go about creating my own XML. I've already written down the following but I need to know how to correctly define this as XML. E.g. Is there a namespace or something I need to add to the top of the file to create valid XML?
<images>
<image>
<id>1</id>
<source>images/slide1.jpg</source>
<alt>This is an image</alt>
<title>caption1</title>
<href>http://url.com</href>
<image>
<image>
<id>3</id>
<source>images/slide3.jpg</source>
<alt>This is an image</alt>
<title>caption3</title>
<href>http://url.com</href>
<caption>This is an example of a HTML caption.</caption>
<image>
<image>
<id>2</id>
<source>images/slide2.jpg</source>
<alt>This is an image</alt>
<title>caption2</title>
<href>http://url.com</href>
<image>
</images>
So thats potentially the XML defined to store the slider details.
Now I need to read the XML to generate the HTML:
All the images have to be stored between the <div id="slider"> images </div> and the captions have to be defined outside of this .
So I need to fetch the images first in the corret order based on the ID <id>. Something like (Not real code).
foreach image as images {
$id = <id>;
$src = <src>;
$title = <title>;
$alt = <alt>;
$href = <href>;
orderby $id;
<!--HERE I NEED TO DO SOME KIND OF if HERE SO THAT ONLY THOSE ELEMENTS
THAT ARE POPULATED IN THE XML ARE USED
-->
echo "<a href='$href'><img src='$src' alt='$alt' title='$title' /></a>";
}
The above would loop 3 times based on the above XML, and output the full HTML ready for a caption.
So to generate the captions I need to create this:
foreach images as image {
if $title = * {
echo "<div id='$title' class='nivo-html-caption'>
This is an example of a HTML caption.
</div>"
}
}
I beleive the Title give to the <img> is the relative to the ID given to the nivo-html-caption ID.
I realise I've written a lot here but just done have the understanding of XML to put the file together in a valid way.
How can I fetch the file using PHP?
How can I loop through the XML to generate the Images in HTML and put them in the correct order?
How can I generate the HTML captions seperatly?
Any help greatly appreciated!
Personally, I'd use JSON to store this information as its very simplified. Using php or perl, you just loop through the array, and this information can be pushed to the users browser if need be.
All of this can be found on David Powers's book OOP php, these examples are his in the book fyi:
How can I fetch the file using PHP?
$xml = simplexml_load_file($location);
How can I loop through the XML to generate the Images in HTML and put them in the correct order?
example of xml structure:
<book isbn13="978-1-43020-991-1">
<title>PHP Object Oriented Solutions</title>
<author>David Powers</author>
<publisher>friends of ED</publisher>
<description>Introduces the key concepts . . . </description>
</book>
ex:
$xml = simplexml_load_file('inventory.xml');
echo $xml->book[0]->title;
And it should show you, PHP Object Oriented Solutions
ex:
$xml = simplexml_load_file('inventory.xml');
foreach ($xml->book as $book) {
echo $book->title . '<br />';
}
This loops through each books if you have more than one .
How can I generate the HTML captions seperatly?
I assume you can have a separate foreach loop that is used for grabbing the captions from the xml.
Yeah, I think this is enough for you to google tutorial on simplexml functions and stuff. Good luck.
Oh, it can make your xml file simpler, less level of nested tags, by embedding html into the xml:
<url> <![CDATA[ URL ]]> </url>
So with that perhaps it'll make parsing your xml file a lot easier.

Categories