<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>
how to use php to echo xml information
First you need to get your XML File. Then just echo what you want. Example:
$file = file_get_contents('http://example.com/rss');
echo $file;
Related
I try to add a single element/single tag without a closing tag to an XML structure with PHP Dom to get something like this:
<?xml version="1.0" encoding="UTF-8"?>
<ndxml>
<credentials>
<identity>User</identity>
<password>Password</password>
<language name="default" modifier="de"/>
</credentials>
</ndxml>
Does anybody know which command I can use to get this?
$domDocument = new \DOMDocument('1.0', 'UTF-8');
$rootNode = $domDocument->createElement('ndxml');
$credentials = $domDocument->createElement('credentials');
$credentials->appendChild(new \DOMElement('identity', 'User'));
$credentials->appendChild(new \DOMElement('password', 'Password'));
$language = $domDocument->createElement('language');
$language->setAttribute('name', 'default');
$language->setAttribute('modifier', 'de');
$credentials->appendChild($language);
$rootNode->appendChild($credentials);
$domDocument->appendChild($rootNode);
echo $domDocument->saveXML();
echo "\n\n";
echo $domDocument->saveXML(null, LIBXML_NOEMPTYTAG);
OUTPUT:
<?xml version="1.0" encoding="UTF-8"?>
<ndxml><credentials><identity>User</identity><password>Password</password><language name="default" modifier="de"/></credentials></ndxml>
<?xml version="1.0" encoding="UTF-8"?>
<ndxml><credentials><identity>User</identity><password>Password</password><language name="default" modifier="de"></language></credentials></ndxml>
I have XML of the form
$xml=<<<EOD
<?xml version="1.0" encoding="utf-8"?>
<CompleteSaleRequest xmlns="urn:ebay:apis:eBLBaseComponents">
<RequesterCredentials>
<eBayAuthToken>AUTH_TOKEN_VALUE</eBayAuthToken>
</RequesterCredentials>
<ItemID>ItemIDVALUE</ItemID>
<TransactionID>TransactionIDVALUE</TransactionID>
<Shipped>1</Shipped>
<Paid>1</Paid>
<Shipment>
<ShipmentTrackingDetails>
<ShipmentTrackingNumber>$trackingNo</ShipmentTrackingNumber>
<ShippingCarrierUsed>UPS</ShippingCarrierUsed>
</ShipmentTrackingDetails>
<ShippedTime>2014-09-30 12:41:59.202303</ShippedTime>
</Shipment>
<ErrorLanguage> string </ErrorLanguage>
EOD;
I want to append more ShipmentTrackingDetails portion using PHP, I need the resulting XML like this:
$xml=<<<EOD
<?xml version="1.0" encoding="utf-8"?>
<CompleteSaleRequest xmlns="urn:ebay:apis:eBLBaseComponents">
<RequesterCredentials>
<eBayAuthToken>AUTH_TOKEN_VALUE</eBayAuthToken>
</RequesterCredentials>
<ItemID>ItemIDVALUE</ItemID>
<TransactionID>TransactionIDVALUE</TransactionID>
<Shipped>1</Shipped>
<Paid>1</Paid>
<Shipment>
<ShipmentTrackingDetails>
<ShipmentTrackingNumber>$trackingNo</ShipmentTrackingNumber>
<ShippingCarrierUsed>UPS</ShippingCarrierUsed>
</ShipmentTrackingDetails>
<ShipmentTrackingDetails>
<ShipmentTrackingNumber>$trackingNo2</ShipmentTrackingNumber>
<ShippingCarrierUsed>UPS</ShippingCarrierUsed>
</ShipmentTrackingDetails>
<ShipmentTrackingDetails>
<ShipmentTrackingNumber>$trackingNo3</ShipmentTrackingNumber>
<ShippingCarrierUsed>UPS</ShippingCarrierUsed>
</ShipmentTrackingDetails>
<ShippedTime>2014-09-30 12:41:59.202303</ShippedTime>
</Shipment>
<ErrorLanguage> string </ErrorLanguage>
EOD;
PHP Source:
$sxe = new SimpleXMLElement($xml);
$track = $sxe->addChild('ShipmentTrackingDetails');
$track->addChild("ShipmentTrackingNumber", "9876");
$track->addChild("ShippingCarrierUsed", "USPS");
I am not getting how to do this using php.
Please help in getting this working.
Thanks!
I got it,
The dynamic part would be:
$sxe = new SimpleXMLElement($xml);
$TrackingDetails = $sxe->Shipment->addChild('ShipmentTrackingDetails');
$TrackingDetails->addChild("ShipmentTrackingNumber", $trackingMore);
$TrackingDetails->addChild("ShippingCarrierUsed", "UPS");
$sxe->asXML("tracking.xml");
$xml1 = $sxe->saveXML();
Where $xml is the original xml to which we want to add new child.
Not too sure what I'm doing wrong with parsing/reading an xml document.
My guess is that it's not standardized, and I'm going to need a different process to read anything from the string.
If that's the case, then I'm rather excited to learn how someone would read the xml.
Here's what I've got, and what I'm doing.
example.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="someurl.php"?>
<response>
<status>Error</status>
<error>The error message I need to extract, if the status says Error</error>
</response>
read_xml.php
<?php
$content = 'example.xml';
$string = file_get_contents($content);
$xml = simplexml_load_string($string);
print_r($xml);
?>
I'm getting no result back from the print_r.
I switched the xml to something more standard, like:
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
...and it worked fine. So I'm sure it's due to a non-standard format, passed back from the source I'm getting it from.
How would I extract the <status> and <error> tags?
Tek has a good answer, but if you want to use SimpleXML, you can try something like this:
<?php
$xml = simplexml_load_file('example.xml');
echo $xml->asXML(); // this will print the whole string
echo $xml->status; // print status
echo $xml->error; // print error
?>
EDIT: If you have multiple <status> and <error> tags in your XML, have a look at this:
$xml = simplexml_load_file('example.xml');
foreach($xml->status as $status){
echo $status;
}
foreach($xml->error as $error){
echo $error;
}
I'm assuming <response> is your root. If it isn't, try $xml->response->status and $xml->response->error.
I prefer to use PHP's DOMDocument class better.
Try something like this:
<?php
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="someurl.php"?>
<response>
<status>Error</status>
<error>The error message I need to extract, if the status says Error</error>
</response>';
$dom = new DOMDocument();
$dom->loadXML($xml);
$statuses = $dom->getElementsByTagName('status');
foreach ($statuses as $status) {
echo "The status tag says: " . $status->nodeValue, PHP_EOL;
}
?>
Demo: http://codepad.viper-7.com/mID6Hp
I have the xml code below:
<?xml version="1.0" encoding="UTF-8"?>
<santamaster>
<paths>
<thename >Agamemnonas</thename>
<soundpath startAt="0">sounds/namesounds/agamemnonas.mp3</soundpath>
<theage startAt="5000">sounds/ages/5.mp3</theage>
<theplace startAt="10000">sounds/places/ipiros.mp3</theplace>
<everyday startAt="15000">sounds/everyday/bravekid.mp3</everyday>
<youwill startAt="20000">sounds/youwill/listeryourparents.mp3</youwill>
<pic1 startAt="0">images/pic1.jpg</pic1>
<pic2 startAt="5000">images/pic2.jpg</pic2>
<pic3 startAt="10000">images/pic3.jpg</pic3>
<videofile>myvideo.mp4</videofile>
</paths>
</santamaster>
i am trying to echo this code through a php file and some variables:
<?php
session_start();
header('Content-type: text/xml');
echo '<?xml version="1.0" encoding="utf-8"?>';
echo'
<santamaster>
<paths>
<thename >Irene</thename>
<soundpath startAt="0">"'.$_SESSION['audiolink'].'"</soundpath>
<theage startAt="5000">"'.$_SESSION['age'].'"</theage>
<theplace startAt="10000">"'.$_SESSION['thelocation'].'"</theplace>
<everyday startAt="15000">"'.$_SESSION['dailylife'].'"</everyday>
<youwill startAt="20000">"'.$_SESSION['YOU_WILL'].'"</youwill>
<pic1 startAt="0">"'.$_SESSION['pimagepath'].'"</pic1>
<pic2 startAt="5000">"'.$_SESSION['eoneimagepath'].'"</pic2>
<pic3 startAt="10000">"'.$_SESSION['etwoimagepath'].'"</pic3>
<videofile>santavideo/sample.mp4</videofile>
</paths>
</santamaster>';
?>
But nothing happend..
Its importan to load the variables in xml through session, the session variables are working.. I need it to load it in an flash movie!
Thank you all!
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Escaping <? on php shorthand enabled server when using require
What I want is that when I make a Ajax get request to domain/xml.php. It return a XML file, so I can use httpRequest.responseXML to parse the XML file.
what I did is:
<?php
$name = 'Just a tester';
?>
<?xml version='1.0' ?>
<name><?php echo $name ?></name>
But the parser gives me an error of the line <?xml version='1.0' ?>, I thought it might be syntax conflict with the php delimiter <?php.
How can I request a url and get xml generated by php?
You have shorttags enabled. This is the default, and as of PHP 5.4, tags are supported everywhere, regardless of shorttags settings.
The problem is that <?xml version='1.0' ?> starts and ends with <? ?>, just like PHP with shorttags.
To get round this use:
echo "<?xml version='1.0' ?>";
on that line.
Why are you trying to embed PHP variables into XML instead of generating the XML with PHP?
Example (xml.php):
<?php
header('Content-type: text/xml; charset=utf-8');
//Your Data
$persons = array(array('name'=>'bob','age'=>20,'sex'=>'M'),
array('name'=>'steve','age'=>26,'sex'=>'M'),
array('name'=>'jen','age'=>33,'sex'=>'F'),
);
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><persons/>');
foreach ($persons as $person) {
$node = $xml->addChild('person');
foreach($person as $key=>$value){
$node->addChild($key, $value);
}
}
//DOMDocument to format code output
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml->asXML());
echo $dom->saveXML();
/* OUTPUT
<?xml version="1.0" encoding="UTF-8"?>
<persons>
<person>
<name>bob</name>
<age>20</age>
<sex>M</sex>
</person>
<person>
<name>steve</name>
<age>26</age>
<sex>M</sex>
</person>
<person>
<name>jen</name>
<age>33</age>
<sex>F</sex>
</person>
</persons>
*/
?>
You could try:
<?php
header('Content-type: text/xml; charset=utf-8');
$name = 'Just a tester';
echo "<?xml version='1.0' ?>";
?>
<name><?php echo $name; ?></name>
Change
echo "<?xml...?>";
to
echo '<'."?...?".'>';
Or use Lawerence solution
You should set up your webserver to process not only PHP but XML files also through the PHP preprocessor.
You have short tags enabled. These should be disabled in your php.ini, search for "short_open_tag" and "asp_tag".
If you have an earlier version than PHP 5.3.0 this will probably work (if you only want this for the current document):
<?php ini_set('short_open_tag', 0); ?>