I have a XML with a node called 'e-mail'. I use simplexml_load_file to read the file but when i want to get the row value with $row->e-mail i get just get 0 back.
What's wrong here, all other names work fine so i think it has something to do with 'mail'.
tnx
From the manual
Accessing elements within an XML
document that contain characters not
permitted under PHP's naming
convention (e.g. the hyphen) can be
accomplished by encapsulating the
element name within braces and the
apostrophe.
echo
$xml->movie->{'great-lines'}->line;
So you need something like
$row->{'e-mail'}
This should work:
$row->{'e-mail'}
Related
I am using this PHP library to work with a dom :http://simplehtmldom.sourceforge.net/
I am wanting to access the data-href element of a li element on this page:http://www.spareroom.co.uk/flatshare/bristol/
according to the api reference:
http://simplehtmldom.sourceforge.net/manual_api.htm
This code should work - so long as $res represents the li dom node - which in my case it does:
echo $res->data-href;
However when i run that the echo is "0".... when I would expect to see something like :
"/flatshare/fad_click.pl?fad_id=3248085&search_id=&offset=0&city_id=&flatshare_type=offered&search_results=%2Fflatshare%2Fbristol%2F&"
Can somebody please help me to understand what I am doing wrong
$res->data-href is parse as
$res->data - href
i.e. it's a subtraction, because - is not a valid character in an identifier. Try:
$res->{"data-href"}
Since you are accessing a object keys with special characters have to be quoted and surrounded with {}.
So:
echo $res->data-herf
Should be:
echo $res->{"data-href"}
In all honesty though it probably just easier(and safer) to use the method:
$res->getAttribute("data-href");
I have
this xml file
and i'm trying to access to the value of attribute permission in the tag yt:accessControl in php
echo (string)$xmlyt->entry->children('yt')->{'accessControl'}->attributes()->$actionAttr."------------";
but i have the error
Node no longer exists
Understanding how SimpleXML works and XML generally is greatly beneficial to doing this...
You can discover things through trial and error and end up with something like this:
$sxml=simplexml_load_file('http://gdata.youtube.com/feeds/api/videos/'.$videoID.'?v=2');
$yt = $sxml->children('http://gdata.youtube.com/schemas/2007');
print_r($yt->accessControl->attributes());
print_r($yt->accessControl[4]->attributes());
For instance, this will give you permissions for the first and fifth actions, which happen to be comment and embed ATM (should probably loop through all to identify the ones you're interested instead of relying on the order).
Hope this helps,
aL
I have two lines of XML data that are attributes but also contain data inside then and they are repeating fields. They are being stored in a SimpleXML variable.
<inputField Type="Name">John Doe</inputField>
<inputField Type="DateOfHire">Tomorrow</inputField>
(Clearly this isnt real data but the syntax is actually in my data and I'm just using string data in them)
Everything that I've seen says to access the data like this, ,which I have tried and it worked perfectly. But my data is dynamic so the data isn't always going to be in the same place, so it doesn't fit my needs.
$xmlFile->inputField[0];
$xmlFile->inputField[1];
This works fine until one of the lines is missing, and I can have anywhere from 0 to 5 lines. So what I was wondering was is there any way that I can access the data by attribute name? So potentially like this.
$xmlFile->inputField['Name'];
or
$xmlFile->inputField->Name;
I use these as examples strictly to illustrate what I'm trying to do, I am aware that neither of the above lines of code are syntactically correct.
Just a note this information is being generated externally so I cannot change the format.
If anyone needs clarification feel free to let me know and would be happy to elaborate.
Maybe like this?
echo $xmlFile->inputField->attributest()->Name;
And what you're using? DOMDocument or simplexml?
You don't say, but I assume you're using SimpleXMLElement?
If you want to access every item, just iterate:
foreach ($xmlFile->inputField as $inputField) { ... }
If you want to access an attribute use array notation:
$inputField['Type']
If you want to access only one specific element, use xpath:
$xmlFile->xpath('inputField[#Type="Name"]');
Perhaps you should read through the basic examples of usage in the SimpleXMLElement documentation?
For example you can a grab a data:
$xmlFile = simplexml_load_file($file);
foreach($xmlFile->inputField as $res) {
echo $res["Name"];
}
I'm implementing a php interface to process a .php file containing a bunch of define sentences defining constants with text values to translate by somebody.
The input is parsed sentence by sentence and shown in the interface to the translator who will input the translation in a html textarea and send it to the server
By the end of the process an output file would be generated, identical to the input .php file, only with the define values translated.
E.g. processing an input file 'eng.php' like this:
<?php
define ("SENTENCE_1", "A sentence to translate");
?>
would give the translated output file 'spa.php' like this:
<?php
define ("SENTENCE_1", "The same sentence translated to spanish");
?>
For this I would like to know:
What is the best way to parse the input file to get the constant names and values in an array? (Something like $var[0]['name'] would be "SENTENCE_1" and $var[0]['value'] would be "A sentence to translate")
Would it be possible to get the translation from google translator shown in the input textarea as a suggestion to edit for the person who is translating it? How? (I read google translator api v1 is no longer available and v2 is only available as a paid service. Are there any free alternatives?)
http://php.net/manual/es/function.get-defined-constants.php
What about that?
get_defined_constants doesn't give you exactly the structure you asked for, but it should be sufficient.
define('MY_CONSTANT', 'something');
define('MY_CONSTANT_2', 'another');
$constants = get_defined_constants(true);
$constants = $constants['user'];
print_r($constants);
/**
* array(
* 'MY_CONSTANT' => 'something',
* 'MY_CONSTANT_2' => 'another'
* )
*/
Note that this will be all constants defined in the current scope, which in PHP is gonna be anything defined this request.
Use get_defined_constants() to get the list of all the defined constants.
To get userdefined constant specially
$allconstants = get_defined_constants(true);
print_r($allconstants['user']);
In case anybody needs to read constants' names and values defined in a given .php file into an array of variables without actually defining those constants (E.g. if some different constant with the same name was previously defined, thus giving an error when processing the file with include or require), here is how I did it (Warning: I haven't had any trouble yet, but it's not thoroughly tested, so it can be buggy).
if (file_exists($filename)){
$outf=fopen($filename,'r');
while (($line=fgets($outf))!==false){
if (strpos($line, 'define')!==false){
$parts=explode("\"",implode("\"",explode("'",implode("\\q",explode("\\\"",implode("\\s",explode("\\'",$line)))))));
$name=implode("\\'",explode("\\s",implode("\\\"",explode("\\q",$parts[1]))));
$value=implode("\\'",explode("\\s",implode("\\\"",explode("\\q",$parts[3]))));
$outconstants[$name]=$value;
}
}
}
You can see I assume there's no more than 1 define sentence per line, and that the names and values of the constants are specified as string values using PHP notation (between single (') or double (") quotes.)
Also, escaped quotes (\" or \') are temporarily escaped as \q (\") or \s (\') instead, to properly match the non-escaped ones, and then escaped back as usual once what's in between the non escaped ones is assigned to $name and $value.
The google api problem was solved using microsoft translation api instead (free up to 2.000.000 chars/month): http://msdn.microsoft.com/en-us/library/ff512421.aspx#phpexample
I'm trying to parse this feed: http://musicbrainz.org/ws/1/artist/c0b2500e-0cef-4130-869d-732b23ed9df5?type=xml&inc=url-rels
I want to grab the URLs inside the 'relation-list' tag.
I've tried fetching the URL with PHP using simplexml_load_file(), but I can't access it using $feed->artist->relation-list as PHP interprets "list" as the list() function.
I have a feeling I'm going about this wrong (not much XML experience), and even if I was able to get hold of the elements I want, I don't know how to extract their attributes (I just want the type and target fields).
Can anyone gently nudge me in the right direction?
Thanks.
Matt
Have a look at the examples on the php.net page, they actually tell you how to solve this:
// $feed->artist->relation-list
$feed->artist->{'relation-list'}
To get an attribute of a node, just use the attribute name as array index on the node:
foreach( $feed->artist->{'relation-list'}->relation as $relation ) {
$target = (string)$relation['target'];
$type = (string)$relation['type'];
// Do something with it
}
(Untested)