I have a log file whose content is like the one below
2016-04-07 19:37:48 <order merchant="asc" affiliate="" id="UM9E-C01101518" date="1443723720" event_id="1" ref="GDVJT" alias="asc">
<event type="sale" date="2015-10-01 18:22:00" status_code="SA">
<sale amount="61.45" amount_usd="43.94" method="VISA" currency="CAD" processor="visa"/>
<tax amount="7.37" amount_usd="5.28" currency="CAD"/>
<payout amount="39.89" currency="USD"/>
</event>
<customer>
<name>Frank</name>
<email>frank#gmail.com</email>
<address/>
<region>BC</region>
<country>IN</country>
<zip_postal>V8V1J9</zip_postal>
<phone_number>1231231234</phone_number>
<language>EN</language>
<ip>209.13.233.227</ip>
<currency>CAD</currency>
</customer>
I am trying to extract the value of name and email from this log file.
I am using the below code:
$handle = fopen('vendorOrder.log','r') or die ('File opening failed');
while (!feof($handle)) {
$dd = fgets($handle);
$str = htmlentities($dd, ENT_XHTML);
if(preg_match("/<name>(.*)<\/name>/",$str)){
$txt = getTextBetweenTags($str, "name");
echo $txt;
}
}
fclose($handle);
function getTextBetweenTags($string, $tagname) {
$pattern = "/<$tagname>(.*)<\/$tagname>/";
preg_match($pattern, $string, $matches);
return $matches[0];
}
But it is never matching the name tag and I am not able to get the value between the tags.
I need the output as Frank.
Can someone let me know whats wrong in the code
Use simplexml_load_file() php function.
Example:
$xml=simplexml_load_file("vendorOrder.log") or die("Error: Cannot create object");
And get this value with $xml->customer->name
Maybe your XML are crashed. Try this format.
<order merchant="asc" affiliate="" id="UM9E-C01101518" date="1443723720" event_id="1" ref="GDVJT" alias="asc">
<event type="sale" date="2015-10-01 18:22:00" status_code="SA">
<sale amount="61.45" amount_usd="43.94" method="VISA" currency="CAD" processor="visa"/>
<tax amount="7.37" amount_usd="5.28" currency="CAD"/>
<payout amount="39.89" currency="USD"/>
</event>
<customer>
<datetime>2016-04-07 19:37:48</datetime>
<name>Frank</name>
<email>frank#gmail.com</email>
<address/>
<region>BC</region>
<country>IN</country>
<zip_postal>V8V1J9</zip_postal>
<phone_number>1231231234</phone_number>
<language>EN</language>
<ip>209.13.233.227</ip>
<currency>CAD</currency>
</customer>
</order>
Yup as stated use simplexml_load_file($url) ; to parse xml.
you can find examples here : http://www.w3schools.com/php/php_xml_simplexml_get.asp
Make a correction in original string with something like this:
<?php
$xml_file = 'vendorOrder.log';
$xml_content = file_get_contents( $xml_file );
$xml_content .= "\n</order>";
$xml_content = substr( $xml_content, 20 );
var_dump($xml_content);
$xml=simplexml_load_string($xml_content) or die("Error: Cannot create object");
echo $xml->customer->name;
Related
I can read XML files or strings but not the next:
$str = <<<XML
<Output xmlns="nice.uniform://" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Data i:nil="true"/>
<Result xmlns:a="http://nice.uniform/CLSAPI3">
<a:ResultCode>SUCCESS</a:ResultCode>
<a:ResultMessage>OK</a:ResultMessage>
<a:ResultCodeEx>CLS_SE_SUCCESS</a:ResultCodeEx>
</Result>
<Exception i:nil="true" xmlns:a="http://schemas.datacontract.org/2004/07/System"/>
</Output>
XML;
My PHP code to read a node of XML file is:
$Output = new SimpleXMLElement($str);
echo $Output->Result->{'a:ResultCodeEx'};
Also, I've tried:
$xmlResponse = simplexml_load_file('file.xml');
foreach($xmlResponse->Result as $xmlEntry)
{
echo $xmlEntry->{'a:ResultCodeEx'};
}
//or
$blocks = $xmlResponse->xpath('//Output');
print_r($blocks);
Can you help me?
The document uses several namespaces. The element ResultCodeEx belongs to the namespace a. You can use XPath, but you need to register the namespace a before the query:
$Output = new SimpleXMLElement($str);
$Output->registerXPathNamespace ('a', 'http://nice.uniform/CLSAPI3');
$result = $Output->xpath('//a:ResultCodeEx/text()');
echo $result[0]; // CLS_SE_SUCCESS
I am testing out some things with reading XML using PHP. The below is a sample of the XML File:
<?xml version="1.0" encoding="UTF-8" ?>
<BroadcastData creationDate="20140326085217">
<ScheduleData>
<ChannelPeriod beginTime="20140326090000" endTime="20140402044500">
<ChannelId>Rai Uno</ChannelId>
<Event beginTime="20140326090000" duration="1800">
<EventId>260852180006</EventId>
<EventType>P</EventType>
<EpgProduction>
<EpgText language="eng">
<Name>Unomattina storie vere</Name>
</EpgText>
</EpgProduction>
</Event>
<Event beginTime="20140326093000" duration="1500">
<EventId>260852180007</EventId>
<EventType>P</EventType>
<EpgProduction>
<EpgText language="eng">
<Name>Unomattina Verde</Name>
</EpgText>
</EpgProduction>
</Event>
This is the PHP Script I built, however notthing is showing on when I run the PHP file.
<?php
$completeurl ="test.xml";
$xml = simplexml_load_file($completeurl);
$info = $xml->BroadcastData->ScheduleData->ChannelPeriod->ChannelId;
for ($i = 0; $i++) {
$begintime = $info[$i]->Event->attributes()->beginTime;
echo "<p>Channel: ".$info."<br/>"."Begin Time: ".$begintime."</p>";
}
?>
Many thanks for your help guys !
You should iterate over each channel period rather than channel id (which is a sub element anyway):
$doc = simplexml_load_file($completeurl);
foreach ($doc->ScheduleData->ChannelPeriod as $channelPeriod) {
$channelId = (string)$channelPeriod->ChannelId;
foreach ($channelPeriod->Event as $event) {
$beginTime = $event['beginTime'];
printf('<p>Channel: %s<br />Begin Time: %s</p>', $channelId, $beginTime);
}
}
use following function to convert the xml string into array
json_decode(json_encode((array)simplexml_load_string($sXML)),1);
I am practicing on XML and PHP. I can not find out how to delete the whole xml node. Here is my XML:
<?xml version="1.0" encoding="UTF-8"?>
<items>
<playlist_name>Channel List</playlist_name>
<category>
<category_id>1</category_id>
<category_title>HD</category_title>
</category>
<channel>
<title>VTC HD1</title>
<stream_url><![CDATA[http://203.162.16.22:80/lives/origin01/vtchd1.isml/vtchd1.m3u8]]></stream_url>
<logo_30x30>vtchd1.jpg</logo_30x30>
<category_id>1</category_id>
</channel>
<channel>
<title>VTC HD2</title>
<stream_url><![CDATA[http://203.162.16.22:80/lives/origin01/vtchd2.isml/vtchd2.m3u8]]></stream_url>
<logo_30x30>vtchd2.jpg</logo_30x30>
<category_id>1</category_id>
</channel>
</items>
And here is the code of two function which I am having problem, delete() and edit()
<?php
function delete_channel()
{
$file = "nStream.xml";
$fp = fopen($file, "rb") or die("cannot open file");
$str = fread($fp, filesize($file));
$xml = new DOMDocument();
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$xml->loadXML($str) or die("Error");
// original
// echo "<xmp>OLD:\n". $xml->saveXML() ."</xmp>";
// get document element
$root = $xml->documentElement;
$fnode = $root->firstChild;
//get a node
$ori = $fnode->childNodes->item(0);
// remove
$fnode->removeChild($ori);
// echo "<xmp>NEW:\n". $xml->saveXML() ."</xmp>";
}
function edit_channel()
{
echo 'Go Edit';
}
For the delete_channel(), nothing is deleted when I run that function. I want each time I use that function, one in xml file ll be deleted.
delete a <channel> with simplexml:
$xml = simplexml_load_file($filename);
// select the channel to be deleted by title
$channel = $xml->xpath("//channel[title='VTC HD2']");
// delete it!
unset($channel[0][0]);
// save it
$xml->asXML($filename);
see it working: https://eval.in/37084
in my site in PHP I want to read an xml file like this:
<?xml version="1.0" standalone="yes"?>
<RETURNDATA lang="it-IT" type="COR" xsi:noNamespaceSchemaLocation="http://xmlv5test.travco.co.uk/trlink/schema/CountryRequestV6Rcv.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<MESSAGE>All Countries details and relevant city details</MESSAGE>
<DATA COUNTRY_CODE="ABW" CURRENCY_CODE="EUR">
<COUNTRY_NAME>Aruba</COUNTRY_NAME>
<CURRENCY_NAME>euro</CURRENCY_NAME>
<COUNTRY_CITIES>
<CITY_DATA CITY_CODE="AUA">
<CITY_NAME>Aruba</CITY_NAME>
</CITY_DATA>
</COUNTRY_CITIES>
</DATA>
<DATA COUNTRY_CODE="ALB" CURRENCY_CODE="EUR">
<COUNTRY_NAME>Albania</COUNTRY_NAME>
<CURRENCY_NAME>euro</CURRENCY_NAME>
<COUNTRY_CITIES>
<CITY_DATA CITY_CODE="TIA">
<CITY_NAME>Tirana</CITY_NAME>
</CITY_DATA>
</COUNTRY_CITIES>
</DATA>
<DATA COUNTRY_CODE="ARE" CURRENCY_CODE="EUR">
<COUNTRY_NAME>Emirati Arabi Uniti</COUNTRY_NAME>
<CURRENCY_NAME>euro</CURRENCY_NAME>
<COUNTRY_CITIES>
<CITY_DATA CITY_CODE="DXB">
<CITY_NAME>Dubai</CITY_NAME>
</CITY_DATA>
<CITY_DATA CITY_CODE="AAI">
<CITY_NAME>Al Ain</CITY_NAME>
</CITY_DATA>
<CITY_DATA CITY_CODE="FJR">
<CITY_NAME>Fujaira</CITY_NAME>
</CITY_DATA>
<CITY_DATA CITY_CODE="SSH">
<CITY_NAME>Sharja</CITY_NAME>
</CITY_DATA>
<CITY_DATA CITY_CODE="RKT">
<CITY_NAME>Ras al-Khaimah</CITY_NAME>
</CITY_DATA>
<CITY_DATA CITY_CODE="AUH">
<CITY_NAME>Abu Dhabi</CITY_NAME>
</CITY_DATA>
</COUNTRY_CITIES>
</DATA>
</RETURNDATA>
I want to enter in each node name DATA and take:
COUNTRY_CODE
CURRENCY_CODE
COUNTRY_NAME
CURRENCY_NAME
And all ountry cities code and name into an array associative.
I have tried with SimpleXML, but the XML is dynamic and I wanto to optimize my cycle because I can have a very big and large XML (this is only a little part of It).
$xml_str = file_get_contents('xml/country.xml');
$xml = new SimpleXMLElement($xml_str);
echo $xml->getName(), PHP_EOL;
foreach($xml as $name => $part) {
echo "$name: $part", PHP_EOL;
}
I want to create a very otpimize cycle to take my value
Use the following style to acces only the <DATA> child elements in the root element:
foreach ($xml->DATA as $name => $part) {
Also please check the SimpleXML Introduction it should have some good examples for you showing different ways how to do the basic stuff with SimpleXML. The documentation is pretty good.
you could use the XML parser directly, this would more than likely be the most efficient way
set_time_limit(0);
define('__BUFFER_SIZE__', 131072);
define('__XML_FILE__', 'pf_1360591.xml');
function elementStart($p, $n, $a) {
//handle opening of elements
}
function elementEnd($p, $n) {
//handle closing of elements
}
function elementData($p, $d) {
//handle cdata in elements
}
$xml = xml_parser_create();
xml_parser_set_option($xml, XML_OPTION_TARGET_ENCODING, 'UTF-8');
xml_parser_set_option($xml, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($xml, XML_OPTION_SKIP_WHITE, 1);
xml_set_element_handler($xml, 'elementStart', 'elementEnd');
xml_set_character_data_handler($xml, 'elementData');
$f = fopen(__XML_FILE__, 'r');
if($f) {
while(!feof($f)) {
$content = fread($f, __BUFFER_SIZE__);
xml_parse($xml, $content, feof($f));
unset($content);
}
fclose($f);
}
I have a small requirement where I need to create a XML file on the fly. It was no problem for me to create a normal xml file which would be looking like this:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<item>
<name></name>
</item>
</root>
But my requirement is such that I need to create a XML file whose output is:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<item>
<name url = "C:\htdocs\proj1\source_file1"/>
<name url = "C:\htdocs\proj1\source_file2"/>
<name url = "C:\htdocs\proj1\source_file3"/>
</item>
</root>
I have tried in this fashion:
<?php
$domtree = new DOMDocument('1.0', 'UTF-8');
$domtree->formatOutput = true;
$xmlRoot = $domtree->createElement("root");
$xmlRoot = $domtree->appendChild($xmlRoot);
$item = $domtree->createElement("item");
$item = $xmlRoot->appendChild($item);
$name= $domtree->createElement("name");
$name = $item->appendChild($name);
$sav_xml = $domtree->saveXML();
$handle = fopen("new.xml", "w");
fwrite($handle, $sav_xml);
fclose($handle);
?>
But I wanted to append/add the url="path" to my elements. I have tried declaring variables with url and path but this throws me errors like:
Uncaught exception 'DOMException' with message 'Invalid Character Error'
Any ideas how to approach this problem!
Thanks
You just have to declare that attributes via php DOM:
...
$name= $domtree->createElement("name");
$urlAttribute = $domtree->createAttribute('url');
$urlAttribute->value = 'C:\htdocs\proj1\source_file1';
$name->appendChild($urlAttribute);
$item->appendChild($name);
...
Link to DOMDocument docs