hello i have tried and nothing will happen...
i will count the childs from an xml file via php
everthing is ok but i dont get, - load correctly this stupid xml file into my page =
here's the script simply --
$url123 = 'http://steamcommunity.com/id/ProJaCore/stats/GarrysMod/?xml=1';
$data123 = file_get_contents($url123);
$xml = simplexml_load_string($data123);
$elem = new SimpleXMLElement($xml);
foreach ($elem as $achievements) {
print $achievements->count().'<br>';
}
Do this:
$url123 = 'http://steamcommunity.com/id/ProJaCore/stats/GarrysMod/?xml=1';
$data123 = file_get_contents($url123);
$elem = new SimpleXMLElement($data123);
foreach ($elem as $achievements) {
print $achievements->count().'<br>';
}
In your code you're creating a SimpleXMLElement object in $xml, then trying to create another one in $elem using the $xml object.
See the complete reference: http://www.php.net/manual/en/book.simplexml.php
Related
Hi im trying to pass a string from xml file to php variable
xml source : https://airdropalert.com/rssfeed/active
$xml = simplexml_load_file("active.xml") or die("Error: Cannot create object");
foreach($xml->entry as $entry)
{
echo $entry->media->icon->imageurl;}
Above Code is not getting output. please help
You had a typo in your code, use
imageUrl
not
imageurl
, tested following code to work:
<?php
$xml = file_get_contents("https://airdropalert.com/rssfeed/active");
$xmldata = simplexml_load_string($xml);
foreach($xmldata->children() as $entry) {
echo $entry->media->icon->imageUrl;
}
?>
I have the following code to read an XML file which works well when the URL is available:
$url = 'http://www1.blahblah.com'."param1"."param2";
$xml = file_get_contents($url);
$obj = SimpleXML_Load_String($xml);
How can I change the above code to cycle through a number of different URL's if the first one is unavailable for any reason? I have a list of 4 URL's all containing the same file but I'm unsure how to go about it.
Replace your code with for example this
//instead of simple variable use an array with links
$urls = [ 'http://www1.blahblah.com'."param1"."param2",
'http://www1.anotherblahblah.com'."param1"."param2",
'http://www1.andanotherblahblah.com'."param1"."param2",
'http://www1.andthelastblahblah.com'."param1"."param2"];
//for all your links try to get a content
foreach ($urls as $url) {
$xml = file_get_contents($url);
//do your things if content was read without failure and break the loop
if ($xml !== false) {
$obj = SimpleXML_Load_String($xml);
break;
}
}
I'm creating a tool that works with file strings and I need to get the line number where a node is found. It is, I have this:
$dom = new DOMDocument('1.0');
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
foreach ($xpath->query("//text()") as $q) {
// $line = WHAT???
$strings[trim($q->nodeValue)] = $line;
}
and I need to know in which line begins the string I'm storing in $strings array. Is it possible to get it?
Each DOMNode object has a getLineNo() function that returns this. In your case it's a DOMText object that extends from DOMNode:
foreach ($xpath->query("//text()") as $q) {
$line = $q->getLineNo();
$strings[trim($q->nodeValue)] = $line;
}
You might need to upgrade to PHP 5.3 if you have not yet to make use of that function.
I want to add/display data from querying from the database and add it into an XML file.
Example, I have a table_persons which has a name and age. I create a mysql query to get its name and age. Then simply put the data(name and age of persons) into an XML file.
How would you do that? Or is it possible?
I suggest you use DomDocument and file_put_contents to create your XML file.
Something like this:
// Create XML document
$doc = new DomDocument('1.0', 'UTF-8');
// Create root node
$root = $doc->createElement('persons');
$root = $doc->appendChild($root);
while ($row = mysql_fetch_assoc($result)) {
// add node for each row
$node = $doc->createElement('person');
$node = $root->appendChild($node);
foreach ($row as $column => $value) {
$columnElement = $doc->createElement($column);
$columnElement = $node->appendChild($columnElement);
$columnValue = $doc->createTextNode($value);
$columnValue = $columnElement->appendChild($columnValue);
}
}
// Complete XML document
$doc->formatOutput = true;
$xmlContent = $doc->saveXML();
// Save to file
file_put_contents('persons.xml', $xmlContent);
<?php
[snip] //database code here
$f = fopen('myxml.xml', 'a+');
foreach($row = mysqli_fetch_assoc($resultFromQuery))
{
$str = "<person>
<name>{$row['name']}</name>
<age>{$row['age']}</age>
</person>\n";
fwrite($f, $str);
}
fclose($f);
?>
Assuming you use mysqli, this code works. If not, suit to fit. In the fopen function call, the a+ tells it to open it for reading at writing, placing the pointer at the end of the file.
Best of luck.
I am having some trouble mixing PHP with XML.
I currently have a PHP file that takes variables from the URL string and I need to calculate something based on these variables, and then return the output in an XTML format.
I currently have my main config file that links to my xml generating file:
include('Xml.php');
$x = new xml();
$x->generate();
And my XML generating file is as follows:
<?php
Class XML {
public function generate() {
$doc = new DOMDocument('1.0');
$doc->formatOutput = true;
$root = $doc->createElement('conv');
$root = $doc->appendChild($root);
$at = $doc->createElement('at');
$at = $root->appendChild($at);
$text = $doc->createTextNode("hi");
$text = $at->appendChild($text);
echo $doc->saveXML();
}
}
?>
But this doesn't work - what am I doing wrong here - I know it's probaby obvious but I am new to XML and can't seem to get it working!
Should I be doing it differently? If so ... how?
I've just tested your class with following code:
$xml = new XML();
$xml->generate();
And I've got this result:
[vyktor#grepfruit tmp]$ php test.php
<?xml version="1.0"?>
<conv>
<at>hi</at>
</conv>
So your class works just fine and your error is somewhere else, eg. including wrong file.
Turn on error_reporting and paste errors in comment.