i've googled and found several php extensions for php so i was wondering:
is there already a built-in support for parsing xml in php?
i'd like to convert my projects from asp to php - i got xml data stored in a database - so i need an xml parser for php which can accept xmldata as string (not load-from-file) - any ideas how to do it?
thanks
SimpleXML
To be precise: simplexml_load_string — Interprets a string of XML into an object
object simplexml_load_string ( string $data [, string $class_name = "SimpleXMLElement" [, int $options = 0 [, string $ns [, bool $is_prefix = false ]]]] )
Example:
$string = // your XML string
$xml = simplexml_load_string($string);
Related
I am getting xml parse error while trying to parse a string (with CDATA within CDATA)
var cont = "<op><![CDATA[someData<p><![CDATA[someotherData]]></p></op>";
XElement.Parse(cont);
Error:
The 'op' start tag on line 1 position 2 does not match the end tag of 'p'. Line 1, position 52.
Can we have CDATA within CDATA ? If we can, then why am I getting the error.
Below code works fine (It does not contain CDATA within CDATA).
var cont = "<op><![CDATA[someData]]</op>";
XElement.Parse(cont);
1 <op>
2 <![CDATA[
3 someData
4 <p>
5 <![CDATA[someotherData]]>
6 </p>
7 </op>
When the XML Parser encounters the ]]> in line 5 , it will terminate the first <![CDATA[ it met in line 2 . As a result , you can never have nested CDATA within an CDATA.
CDATA is not designed to hold xmlelements , but to hold character data that might contains characteres such as <, > and so on , which allows us to avoid escaping them as < , > respectively , and to write them and display them in a clean way .
So the content between <![CDATA[ and ]] will be treated as plain text , with no further processing , even if it looks like that there's a hierarchy . In other words , they are plain strings . Let's take your code as an example :
var cont = "<op><![CDATA[ <foo><bar></bar></foo> ]]></op>";
var xml=XElement.Parse(cont);
Here the FirstNode of xml will be a plain text foo><bar></bar></foo> , and the FirstNode of the FirstNode will be null.
Since the parser will always treat the data between <![CDATA[ and ]] as a plain string , there's no "standard" closest valid way to represent them . Just encode them and decode them . For example , we can urlencode the data :
string xmlstr= #"<op><![CDATA[
<helloworld/>
someData%0A%3Cp%3E%0A%3C!%5BCDATA%5BsomeotherData%5D%5D%3E%0A%3C%2Fp%3E
]]></op>";
var xml = XElement.Parse(xmlstr);
var subxmlString=System.Web.HttpUtility.UrlDecode(xml.Value);
// make sure there' must be a root element
var subxml= XElement.Parse($"<root>${subxmlString}</root>");
I am trying to convert the data that I selected from mysql database to json format. I am using Joomla 3.2.1 so that I can use it for my iOS application.
I am getting syntax error unexpected Jresponse t_string error near JResponse.
I would appreciate if anyone can point me in the right direction.
thank you.
<?php
defined ('_JEXEC') or die('');
require_once JPATH_SITE.'/components/com_content/helpers/route.php';
jimport('joomla.application.component.controller');
jimport('joomla.appliction.component.model');
$db = JFactory::getDbo();
// Create a new query object.
$query = $db->getQuery(true);
$query->select($db->quoteName(array('order_id', 'store_name', 'invoice', 'firstname')));
$query->from($db->quoteName('#__mynicetable'));
$query->where($db->quoteName('order_id') );
$query->order('order_id ASC');
$db->setQuery($query);
$row=$db->loadRowList();
print_r($row);
$data =array ($row);
$document= JFactory::getDocument;
$document-> setMimetEncoding('application/json')
JResponse::setHeader('Content-Disposition', 'attachment;filename="'.$view- >getName().'.json"');
echo json_encode($data);
You have some gaps in your code and a missing semi-colon. Try using the following:
$data = array($row);
$app = JFactory::getApplication();
$document = JFactory::getDocument();
$document->setMimetEncoding('application/json');
$app->setHeader('Content-Disposition', 'attachment;filename="my-scratchcomponent.json"');
echo json_encode($data);
We recently purchased a highcharts license and have integrated it with our Grails application.
We're having some difficulty in that we're unable to specify a tool tip formatter in the JSON object that we're returning because it appears that the HighCharts JSON object doesn't conform to the JSON standards.
Specifically, it appears that JSON is not technically allowed to have JavaScript functions as an object property. From the www.json.org website:
A string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes. A character is represented as a single character string. A string is very much like a C or Java string.
So, when we output our formatting string, it gets wrapped in double quotes, like this:
"formatter": "this.x + ': ' + this.y",
Can we get an enhancement where we specify a tooltip (or tooltip-fn) property as a string, which is the name of a javascript function? For example:
"formatter": "myTooltipFn"
which calls a javascript function like:
myTooltipFn(chart) {
return chart.x
}
I've just fixed this after hours of labour. My solution was to add the formatter to the data AFTER the data is sent in JSON format to the browser.
So basically, in the js file that has this line:
$(blah).highcharts(data);
write BEFORE this line:
Data.tooltip.formatter = function() {
//write function here
}
What's the exact equivalent of the PHP function pack in Objective-C ?
For example :
$key = pack('H*', 'ABEFFF' );
$result = hash_hmac("SHA512", "Awesome message", $key)
I am looking for the exact same string that the one returned by the PHP pack function.
I already read this subject : https://stackoverflow.com/a/8950667/2199320
But i can't get to have the correct NSString from the NSData (ASCII, UTF-8, base 64...).
Or if this function really works, i need a way to have a hmac hash for a NSData as a the salt and a NSString as the message...
I tried a lot of differents solutions but none of them were working...
I have two URLs with parameters
http://localhost:8041/Reforge.aspx?name=CyanГ
http://localhost:8041/Reforge.aspx?name=Cyanì
In first URL Firefox encodes last charecter (Г) as %D0%93 (correctly in UTF-8).
In second URL Firefox encodes last character (ì) as %EC (correctly in ISO-8859-1)
ASP.NET MVC can be configured using element in web.config to either assume UTF-8 or ISO-8859-1. But Firefox flips between encodings depending on the context.
Note that UTF-8 can be unambiguously distinguished from Latin-1 encoding.
Is there a way to teach ASP.NET MVC to decode parameter values using either one of the formats?
EDIT: Is there a class that I could use to decode raw query string that would handle encoding correctly? Note - Firefox uses either UTF-8 or Latin-1 encoding - but not both at the same time. So my plan is to try decode manually using UTF-8 and then look for "invalid" character (FFFD), if one is found - try Latin-1 decode.
Example:
Firefox encodes as following:
- v v
http://localhost:8041/Reforge.aspx?name=ArcânisГ
Firefox turns into
http://localhost:8041/Reforge.aspx?name=Arc%C3%A2nis%D0%93`
Notice that UTF8 encoding is used for both non-ASCII characters.
- v
http://localhost:8041/Reforge.aspx?name=Arcâ
Firefox turns into
http://localhost:8041/Reforge.aspx?name=Arc%E2
Notice that ISO-8859-1 (Latin-1) encoding is used for the non-ASCII character.
Here is my working solution, any way to improve on it? Specifically I would rather extend framework instead of handling it inside an action itself.
private string DecodeNameParameterFromQuery(string query) {
string nameUtf8 = HttpUtility.ParseQueryString(query, Encoding.UTF8)["name"];
const char invalidUtf8Character = (char) 0xFFFD;
if (nameUtf8.Contains(invalidUtf8Character)) {
const int latin1 = 0x6FAF;
var nameLatin1 = HttpUtility.ParseQueryString(query, Encoding.GetEncoding(latin1))["name"];
return nameLatin1;
}
return nameUtf8;
}