The user creates a XML-file and stores it in the database as BLOB. Every XML-file gets its own row.
My script should query the Database and retrieve the XML-file of every row.
Every XML-file should be in an own php object and stored in an array. After that I need to access the DOM of every XML to display the data in an html template.
This is what I have got so far:
$stmt = $pdo->query('SELECT xml FROM table');
foreach ($stmt as $row)
{
echo $row['xml'] . "\n";
}
It will output the information without the xml-tags. I wonder why. The problem is, I somehow need to access the DOM.
I read about
simplexml_load_file
but it needs a filepath, which I don't have because the files are stored as BLOB in the DB.
Thank you!
You can use simplexml_load_string instead...
$stmt = $pdo->query('SELECT xml FROM table');
foreach ($stmt as $row)
{
$data = simplexml_load_string($row['xml']);
echo "<pre>".$data->asXML()."</pre>";
}
The reason why you probably don't see the tags is that you are echoing them out to a HTML page, which tries to interpret them as HTML tags.
Not sure about how your doing your foreach as it would more commonly be written as
while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) // Add correct retrieval method your after
{
$data = simplexml_load_string($row['xml']);
echo "<pre>".$data->asXML()."</pre>";
}
This depends on the API and if your using your own class methods though.
Related
I'm having issues attempting to parse an XML file I'm pulling in from another site with SimpleXML. I need to display the rent for certain properties. However, I only want to display the rent for certain properties. I've never seen an XML file structured in this manner: http://dl.dropbox.com/u/1925679/example.xml
First, how would I parse through this file based on
Property->PropertyID->MITS:Identification->MITS:PrimaryID
Then, echo out the Property->Floorplan->MarketRent(Min and Max) based on an ID?
TIA!!!!
// get all properties
$properties = $xml->xpath('//Property');
// get document namesapces in prefix => uri format
$namespaces = $xml->getNamespaces(true);
foreach($properies as $property)
{
// get namespaced nodes
$identification = $property->PropertyID->children($namespaces['MITS']);
$id = $identification->children($namespaces['MITS'])->PrimaryID;
// do your check id is an expected value or whatever and if so then...
foreach($property->Floorplan as $floorplan){
{
echo $floorplan->MarketRent['min'];
echo $floorplan->MarketRent['max'];
}
}
You could probably com up with an xpath query to ONLY select properties with a given id or set of ids straight away so then you would only have to loop over them and invoke the floorplan loop but ill leave that to your investigation :-) I will say though if you go that route, you will need to register the namespaces with xpath i think. Pretty sure thats not automatic.
i am curious to know how can i get the database data in xml format in Zend framework using context switching.
Do i need to compulsorily specify the format in my url, like:
http://localhost/pt/public/index.php/api/v1/users.xml?param1=3
I want to get the format from url (.xml, .json...) and apply the corresponding format to my output automatically.
Currently iam doing this: I get the user data from the database. I get the users marks based on the class id i pass to the url:
$id = $request->getParam('param1'); // get class id param
$users = new Application_Model_DbTable_Users();
$result = $users->fetchData($id);
if(count($result) != 0)
{
$doc = new DOMDocument();
$doc->formatOutput = true;
$root = $doc->createElement("Student");
$doc->appendChild($root);
foreach($result as $details)
{
$root_element = $doc->createElement("Marks");
$root->appendChild($root_element);
$TElement = $doc->createElement("Total");
$TElement->appendChild($doc->createTextNode($details->marks));
$root_element->appendChild($TElement);
}
$xml = $doc->saveXML();
$this->view->xml = $xml;
}
And in the corresponding view script, i have this code:
<?php
header('Content-type: text/xml');
echo $this->xml;
?>
I get the user data and use DOMDocument to write the xml output to the view. But is it possible to automatically generate the XML data from database, without using DOM ?
emaillenin is right, there's nothing ZF can do to convert your data to XML.
But instead of forming XML manually (with DOMDocument and the like), I suggest that you take a look at PEAR XML_Serializer package.
XML_Serializer allows you to transform arrays, objects, etc. to well-formed XML. You can also specify your root name, default tag names, indentation type, etc. So, you're pretty much in control for the resulting XML.
Zend context switching helps in changing your headers, disabling layout and those kind of helps. It does not help in returning the database data as XML data.
The following code can be used to return XML output once you have converted your DB data into XML formatted data with tags.
class OutputController extends Zend_Controller_Action
{
public function xmlAction()
{
$xml = simplexml_load_string($sourceData);
$output = $xml->saveXML();
Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->setNoRender(true);
$this->_helper->layout->disableLayout();
Zend_Layout::getMvcInstance()->disableLayout();
header('Content-Type: text/xml');
echo $output;
exit();
}
}
I will update the answer if you provide more information about how you retrieve the data from DB and what format of XML (the hierarchy of tags) you want in the output.
i want to pass a url value from a php script to another.I have a database in which i have stored some feeds.I have given some weight values to these feeds and a php script grabs a feed url randomly based on their weights.I want to take the feed url which has been grabbed by the script and then pass this url in another php script where it will be parsed with simplepie in order to show its content.
I am posting the codes of the two files right here:
this is the first script which grabs randomly the feed
http://pastebin.com/2ciQ87Es
this is the second script in which i want to pass the value and makes the parsing of the feed
http://pastebin.com/eN5qG29e
Have you something to recommend??
thanks in advance
Would a $_SESSION not suffice?
In the first script:
session_start();
$_SESSION['session_name'] = 'value';
In the second script:
session_start();
print $_SESSION['session_name'];
On second thoughts, could you not pass the value in a query string, to the second page.
second-page.php?key=value
You could wrap grabbing the feed url from the database in a function, and just include that file like any other php file, and then call that function.
//// feedgrabber.php
<?php
function grabber(){
$query = "SELECT * FROM `feeds`";
//takes all the feed that are declared
$result = mysql_query($query);
$data = array();
while($output = mysql_fetch_assoc($result)) {
$data[] = $output; // assigns feeds to an array called $data, one after the other, in they go!
}
return randomchoice($data); // finds a random feed by calling the function
}
?>
and then on the page where you need it:
require('feedgrabber.php');
$feed = grabber();
I have a php function that retrieves tweets from Twitter & returns them in simplexml_load_string. I have to store those into database. Whats is an easy way to store those into database on page load? I have already tried my luck with ajax but json that is returned from twitter seems invalid. thanX a Lot in advance.
Simply store them in the table as XML and then parse them on demand.
// perform query
$res = mysql_query('SELECT * from tweets');
// place xml objects into array
$tweets = array();
while ($row = mysql_fetch_assoc($res)) {
$tweets[] = new SimpleXMLElement($row['data']);
}
SimpleXML
I'm making an interface-website to update a concert-list on a band-website.
The list is stored as an XML file an has this structure :
I already wrote a script that enables me to add a new gig to the list, this was relatively easy...
Now I want to write a script that enables me to edit a certain gig in the list.
Every Gig is Unique because of the first attribute : "id" .
I want to use this reference to edit the other attributes in that Node.
My PHP is very poor, so I hope someone could put me on the good foot here...
My PHP script :
Well i dunno what your XML structure looks like but:
<gig id="someid">
<venue></venue>
<day></day>
<month></month>
<year></year>
</gig>
$xml = new SimpleXmlElement('gig.xml',null, true);
$gig = $xml->xpath('//gig[#id="'.$_POST['id'].'"]');
$gig->venue = $_POST['venue'];
$gig->month = $_POST['month'];
// etc..
$xml->asXml('gig.xml)'; // save back to file
now if instead all these data points are attributes you can use $gig->attributes()->venue to access it.
There is no need for the loop really unless you are doing multiple updates with one post - you can get at any specific record via an XPAth query. SimpleXML is also a lot lighter and a lot easier to use for this type of thing than DOMDOcument - especially as you arent using the feature of DOMDocument.
You'll want to load the xml file in a domdocument with
<?
$xml = new DOMDocument();
$xml->load("xmlfile.xml");
//find the tags that you want to update
$tags = $xml->getElementsByTagName("GIG");
//find the tag with the id you want to update
foreach ($tags as $tag) {
if($tag->getAttribute("id") == $id) { //found the tag, now update the attribute
$tag->setAttribute("[attributeName]", "[attributeValue]");
}
}
//save the xml
$xml->save();
?>
code is untested, but it's a general idea