I am having a question to ask...
I am having a mysql database...
I want to make an XML file for my mysql database using PHP
I did it using newXMLELEMENT function..
Now next thing i want to do is parse my XML file data to HTML Tables & Forms..
Hoe can i add XML data to html textbox and other controls..?
<?php
$xmlDoc = new DOMDocument();
$xmlDoc->load("userimages.xml");
$x = $xmlDoc->documentElement;
foreach ($x->childNodes AS $item)
{
print $item->nodeName . " = " . $item->nodeValue . "<br>";
}
?>
this is my code..i wan tot add the fetched data to forms....but its getting print simply as i am using print function
this is my XML
i want to parse all this data in my forms ex. , etc to disabled textbox,
<ALLIMAGES>
<gal>
<imageid>6</imageid>
<imagetype>Vector</imagetype>
<image>..images/accept.png</image>
<companyname>w845</companyname>
<description>villain</description>
<status>sold</status>
<dou>2014-03-16</dou>
<like>22</like>
<price>212</price>
<color>BLUE</color>
</gal>
<gal>
<imageid>7</imageid>
<imagetype/>
<image/>
<companyname>amit</companyname>
<description>sdfj</description>
<status>Not Sold</status>
<dou>2014-01-01</dou>
<like>0</like>
<price>123</price>
<color/>
</gal>
This is just an illustration and it works as shown.. Spoonfeeding is not good, so you try to explore more with this code..
<?php
$xml=' <ALLIMAGES>
<gal>
<imageid>6</imageid>
<imagetype>Vector</imagetype>
<image>..images/accept.png</image>
<companyname>w845</companyname>
<description>villain</description>
<status>sold</status>
<dou>2014-03-16</dou>
<like>22</like>
<price>212</price>
<color>BLUE</color>
</gal>
<gal>
<imageid>7</imageid>
<imagetype/>
<image/>
<companyname>amit</companyname>
<description>sdfj</description>
<status>Not Sold</status>
<dou>2014-01-01</dou>
<like>0</like>
<price>123</price>
<color/>
</gal>
</ALLIMAGES>';
$xml = simplexml_load_string($xml);
echo "<pre>";
echo "<form method='post' action=''>";
foreach ($xml->gal as $tag)
{
echo "Image ID : <input type='text' value=$tag->imageid />";echo "<br>";
echo "Company Name: <input type='text' value=$tag->companyname />";echo "<br>";
}
echo "</form>";
OUTPUT :
Related
Iam trying to loop a XML file with Foreach but doesn't work. There are like 30 reviews in the XML File but only shows one. It shows the first person in the list but then on the bottom.
Iam trying to get better at PHP so dont know allot about it for now.
This is the code that i use.
<?php
$url = 'https://mobiliteit.klantenvertellen.nl/xml/autorijschool-
wezemer%20' or die ('Niet verbonden');
$xml = simplexml_load_file($url);
foreach ($xml as $rijschool){
echo 'Voornaam: '.$rijschool->beoordeling->voornaam.'<br>';
echo 'Achternaam: '.$rijschool->beoordeling->achternaam.'<br>';
echo 'Woonplaats: '.$rijschool->beoordeling->woonplaats.'<br>';
echo 'Beschrijving: '.$rijschool->beoordeling->beschrijving.'<br>';
echo 'Aanbeveling: '.$rijschool->beoordeling->aanbeveling.'<br>';
echo 'Service: '.$rijschool->beoordeling->service.'<br>';
echo 'Deskundigheid: '.$rijschool->beoordeling->deskundigheid.'<br>';
echo 'Prijskwaliteit: '.$rijschool->beoordeling-
>prijskwaliteit.'<br>';
echo 'Gemiddelde: '.$rijschool->beoordeling->gemiddelde.'<br>'.'<br>';
}
?>
Edit: here is the XML file Link https://mobiliteit.klantenvertellen.nl/xml/autorijschool-wezemer%20
And here is what iam getting what current code shows
I think this is what you're trying to do:
<?php
$url = 'https://mobiliteit.klantenvertellen.nl/xml/autorijschool-wezemer%20';
$xml = simplexml_load_file($url);
foreach ($xml->beoordelingen->beoordeling as $rijschool){
echo 'Voornaam: '.$rijschool->voornaam.'<br>';
echo 'Achternaam: '.$rijschool->achternaam.'<br>';
echo 'Woonplaats: '.$rijschool->woonplaats.'<br>';
echo 'Beschrijving: '.$rijschool->beschrijving.'<br>';
echo 'Aanbeveling: '.$rijschool->aanbeveling.'<br>';
echo 'Service: '.$rijschool->service.'<br>';
echo 'Deskundigheid: '.$rijschool->deskundigheid.'<br>';
echo 'Prijskwaliteit: '.$rijschool->prijskwaliteit.'<br>';
echo 'Gemiddelde: '.$rijschool->gemiddelde.'<br>'.'<br>';
}
?>
The problem you are having is that your foreach is iterating over the topmost node, but you want to iterate over a node lower down in the tree.
I have an xml file like this
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<Job>
<JobInfo>
<User>abc</User>
<Computer>acb</Computer>
<Started>2018/04/21-21:58:30:0182-06</Started>
<Ended>2018/04/21-23:10:10:0093-06</Ended>
</JobInfo>
<JobFlags>
<Active>Yes</Active>
<Complete>Yes</Complete>
</JobFlags>
</Job>
I use simplexml_load_file to load file and print out User, Computer and Complete attribute.
$xml=simplexml_load_file("abc.xml") or die("Error: Cannot create object");
foreach($xml->children() as $xm) {
echo $xm->User . "<br>";
echo $xm->Computer . "<br>";
echo $xm->Complete . "<br>";
But it only print out User and Computer. The result for Complete is empty.
Please help me with this, thank you!
The main issue is that you are trying to mix the fields from different elements. You can see from the original XML that <User> and <Computer> are in the <JobInfo> element and <Complete> is in the <JobFlags> element. This isn't a problem, but when you use your foreach loop, you go through each child element of <Job> and output all of the values for each element. If you change your loop to...
foreach($xml->children() as $tag => $xm) {
echo "element=".$tag . "<br>";
echo $xm->User . "<br>";
echo $xm->Computer . "<br>";
echo $xm->Complete . "<br>";
}
You get (please excuse wrong markup, I wanted the layout)...
element=JobInfoabcacbelement=JobFlagsYes
If instead you just need those three pieces of information from the document, you could access them using the full path and so just pick out the details your after without using a foreach...
echo $xml->JobInfo->User . "<br>";
echo $xml->JobInfo->Computer . "<br>";
echo $xml->JobFlags->Complete . "<br>";
Gives you.
abcacbYes
simplexml is a bit outdated. Not sure that a <complete> tag is valid, xml, it may be the issue.
If you can, try to use php-dom instead.
https://php.net/manual/en/book.dom.php
Example to retrieve all the tags texts.
#!/usr/bin/php
<?php
$remote =<<<'EOF'
<JobInfo>
<User>abc</User>
<Computer>acb</Computer>
<Started>2018/04/21-21:58:30:0182-06</Started>
<Ended>2018/04/21-23:10:10:0093-06</Ended>
</JobInfo>
<JobFlags>
<Active>Yes</Active>
<Complete>Yes</Complete>
</JobFlags>
EOF;
$i = 0;
$doc = new DOMDocument();
$doc->loadHTML($remote);
foreach($doc->getElementsByTagName('*') as $elem) {
echo $i++.": ".$elem->textContent;
}
The package bellong to php-xml:
Package php-dom is a virtual package provided by: php7.2-xml
Alternatives: https://php.net/manual/en/refs.xml.php
Try to wrap all elements into a parent tag:
<?xml version="1.0"?>
<test>
<JobInfo>
<User>abc</User>
<Computer>acb</Computer>
<Started>2018/04/21-21:58:30:0182-06</Started>
<Ended>2018/04/21-23:10:10:0093-06</Ended>
</JobInfo>
<JobFlags>
<Active>Yes</Active>
<Complete>Yes</Complete>
</JobFlags>
</test>
and then reference in your php:
<?php
$xml=simplexml_load_file("abc.xml") or die("Error: Cannot create object");
echo $xml->JobInfo->User . "<br>";
echo $xml->JobInfo->Computer . "<br>";
echo $xml->JobFlags->Complete . "<br>";
?>
It worked here.
I think it's just a matter of making a correct reference to the entire hierarchical structure of the tags.
See this example taken from http://php.net/manual/en/function.simplexml-load-file.php:
<?php
$xml = '<?xml version="1.0" encoding="UTF-8" ?>
<rss>
<channel>
<item>
<title><![CDATA[Tom & Jerry]]></title>
</item>
</channel>
</rss>';
$xml = simplexml_load_string($xml);
// echo does the casting for you
echo $xml->channel->item->title;
// but vardump (or print_r) not!
var_dump($xml->channel->item->title);
// so cast the SimpleXML Element to 'string' solve this issue
var_dump((string) $xml->channel->item->title);
?>
Above will output:
Tom & Jerry
object(SimpleXMLElement)#4 (0) {}
string(11) "Tom & Jerry"
Reference:
https://stackoverflow.com/a/16972780/5074998
http://php.net/manual/en/function.simplexml-load-file.php
It works just fine for me:
abc
acb
Yes
You might be confused because you are expecting it all on three consecutive lines. Try outputting it like this instead and it may help you visualize that it is echoing all three of your requested elements (whether they exist or not) for two loops (since the XML has two children):
foreach($xml->children() as $xm) {
echo "Current Element: " . $xm->getName() . "<br />";
echo "User: " . $xm->User . "<br />";
echo "Computer: " . $xm->Computer . "<br />";
echo "Complete: " . $xm->Complete . "<br />";
}
which will output:
Current Element: JobInfo
User: abc
Computer: acb
Complete:
Current Element: JobFlags
User:
Computer:
Complete: Yes
As Nick suggested in his comment, if you don't want the <br /> breaks to print when the element doesn't exist, you can use isset like:
foreach($xml->children() as $xm) {
echo isset($xm->User) ? "{$xm->User}<br />" : '';
echo isset($xm->Computer) ? "{$xm->Computer}<br />" : '';
echo isset($xm->Complete) ? "{$xm->Complete}<br />" : '';
}
Or, as NigelRen recommended in their answer, you could skip using children() if you know the full path to the elements you need, and just use those paths instead, like:
echo $xml->JobInfo->User . "<br />";
echo $xml->JobInfo->Computer . "<br />";
echo $xml->JobFlags->Complete . "<br />";
The underlying issue being that when you were echoing out $xm->Complete while traversing the JobInfo element, it was outputting just the <br /> because $xml->JobInfo->Complete does't exist.
I'm have to build a "simple" application that will reads an xml file, prompts a user to choose a "response activity" in relation to what's on that file and send the whole thing (what was loaded + the activity chosen by the user) to a client over http in a new xml file.
So my questions is:
how can i add what's been loaded as well as the user chosen response activity onto a new xml file? and how do i send it to anyaddress.com?
i've also been told to use rest webservices, and although i have found a lot of information and examples online, nothing seem to be relevant to what i'm trying to do.
As you may have guessed, i'm new to all this. here' what i've done so far:
<?php
//reader.php
// Load the xml file and show what's on it
$xml = simplexml_load_file('xmlfile.xml')
or die("Could not open file!<hr /> ");
foreach($xml->children() as $child) {
foreach($child->children() as $young) {
echo $young->getName() . ": " . $young . "<br />";
}
}
// call the activity list according to what's on the xml file
if ($child->ACTIVITY == 'activity import') {
echo "<br/>" . file_get_contents('interface.php');
}
elseif($child->ACTIVITY == 'activity import special'){
echo "<br/>" . file_get_contents('interface1.php');
}
elseif($child->ACTIVITY == 'activity export'){
echo "<br/>" . file_get_contents('interface2.php');
}
else {
echo "<br/>" . 'incorrect activity';
}
// print the selected activity on page
if (isset($_POST['submit1'])) {
$selected_radio = $_POST['group1'];
print $selected_radio;
}
?>
this is one of the 3 forms i have for user input interface2.php
<form name="serv1" action="reader.php" method="POST">
<div align="left"><br>
<input type="radio" name="group1" value="OUTBOUND"> OUTBOUND<br/>
<input type="radio" name="group1" value="DONTPROCESS" checked> DON'T PROCESS<br/><br/>
<input type="submit" name="submit1" value="Return">
</div>
</form>
Any kind of help will be much appreciated.
You can perform edits on an XML file e.g. with SimpleXML or with DOMDocument.
You could post the result to another server with CURL.
I'm writing a custom plugin for use in my website using Jw player adtonomy plugin which loads adverts from XML files. However I do not know how to update the nodes in the files. I have tried using simple xml but I don't know how to update. Any implementation is highly regarded. Thanks. Here is the xml file.
<xml>
<plugins>adtimage</plugins>
<adtimage.graphic>http://mysite/default.png</adtimage.graphic>
<adtimage.link>http://targetsite.com</adtimage.link>
<adtimage.positions>pre,post</adtimage.positions>
<adtimage.onpause>true</adtimage.onpause>
<adtimage.txt>Advertisement</adtimage.txt>
<adtimage.btntxt>Click to continue with video</adtimage.btntxt>
</xml>
my code is here.
<?php
$xml = simplexml_load_file("test.xml");
echo $xml->getName() . "<br />";
foreach($xml->children() as $child)
{
echo $child->getName() . ": " . $child . "<br />";
/*
TO DO
change the node value of
<adtimage.graphic>http://mysite/default.png</adtimage.graphic>
to
<adtimage.graphic>http://stackoverflow.com</adtimage.graphic>
*/
}
?>
You can use this
//Load XML
$xml = simplexml_load_file("test.xml");
// Modify a node
$xml->{"adtimage.graphic"} = 'http://stackoverflow.com/adtimage.graphic';
// Saving the whole modified XML to a new filename
$xml->asXml('updated.xml');
Example Output 'updated.xml'
<?xml version="1.0"?>
<xml>
<plugins>adtimage</plugins>
<adtimage.graphic>http://stackoverflow.com/adtimage.graphic</adtimage.graphic>
<adtimage.link>http://targetsite.com</adtimage.link>
<adtimage.positions>pre,post</adtimage.positions>
<adtimage.onpause>true</adtimage.onpause>
<adtimage.txt>Advertisement</adtimage.txt>
<adtimage.btntxt>Click to continue with video</adtimage.btntxt>
</xml>
Input is a URL like this http://ws.geonames.org/children?geonameId=6255148 and I would like to receive the XML file in a SimpleXMLElement for example?
Try this as your base point:
<?php
$xml = simplexml_load_file("http://ws.geonames.org/children?geonameId=6255148");
//print_r($xml);
foreach($xml->geoname as $geo)
{
echo $geo->toponymName . "<br />";
}
?>