This works
$feed = $xml->xpath('//room[#id="103"]');
I am trying to replace 103 by a variable namely $id.
I tried
$feed = $xml->xpath('//room[#id=$id]');
and
$feed = $xml->xpath('//room[#id=".$id."]');
None work.
What is the appropriate syntax for putting a variable in xpath?
When you writed $id in string, php use it like string. You should close quote like
$feed = $xml->xpath('//room[#id="'.$id.'"]');
Or write variable in {}
$feed = $xml->xpath('//room[#id="{$id}"]');
your question already answer from Mohammad
but i give a more fully example with [0] at the end of the line to avoid more rows for some usages
$feed = $xml->xpath('//room[#id="'.$id.'"])[0];
also if you want result from xml with some others values inside to the root where use #attribute
$day= $_GET['Day'];
$feed = $xml->xpath('//room[#id="'.$id.'"]/Day'.$day.'')[0];
this mean if you have a xml file like harder code you can cal a day dynamic
Related
I want to get the last part of an url that looks like this:
http://localhost:8888/blog/public/index.php/categories/Horror
I've tried it with
$endOfUrl = end(explode('/',$url));
but the thing is I get a notice that "Only variables should be passed by reference"
I need this "Horror" to get it's ID in my database and get all the posts with this id, since I'm trying to code a blog to get experience with php.
Another question linked to this: Is it possible to make it dynamic so it can be used for all the other categories as well? Or do I have to do this for every single category?
I'm new to the world of php so I would really appreciate it if someone could help me on this.
Try like this way for end() but If I were you I will try basename() to get my job done.
<?php
$url = 'http://localhost:8888/blog/public/index.php/categories/Horror';
$exploded = explode('/',$url);
$endOfUrl = end($exploded);
echo $endOfUrl;
?>
Reason why it is not working on single line:
end() requires a reference, because it modifies the internal
representation of the array (i.e. it makes the current element pointer
point to the last element).The result of explode('.', $url) cannot be
turned into a reference and this is a restriction in the PHP language itself.
DEMO: https://3v4l.org/ttKui
Using basename(),
$url = 'http://localhost:8888/blog/public/index.php/categories/Horror';
echo basename($url);
DEMO: https://3v4l.org/pt2cQ
Consider this simple piece of code, working normally using the PHP Simple HTML DOM Parser, it outputs current community.
<?php
//PHP Simple HTML DOM Parser from simplehtmldom.sourceforge.net
include_once('simple_html_dom.php');
//Target URL
$url = 'http://stackoverflow.com/questions/ask';
//Getting content of $url
$doo = file_get_html($url);
//Passing the variable $doo to $abd
$abd = $doo ;
//Trying to find the word "current community"
echo $abd->find('a', 0)->innertext; //Output: current community.
?>
Consider this other piece of code, same as above but I add an empty space to the parsed html content (in the future, I need to edit this string, so I just added a space here to simplify things).
<?php
//PHP Simple HTML DOM Parser from simplehtmldom.sourceforge.net
include_once('simple_html_dom.php');
//Target URL
$url = 'http://stackoverflow.com/questions/ask';
//Getting content of $url
$doo = file_get_html($url);
//Passing the variable $url to $doo - and adding an empty space.
$abd = $doo . " ";
//Trying to find the word "current community"
echo $abd->find('a', 0)->innertext; //Outputs: nothing.
?>
The second code gives this error:
PHP Fatal error: Call to undefined function file_get_html() in /home/name/public_html/code.php on line 5
Why can't I edit the string gotten from file_get_html? I need to edit it for many important reasons (like removing some scripts before processing the html content of the page). I also do not understand why is it giving the error that file_get_html() could not be found (It's clear we're importing the correct parser from the first code).
Additional note:
I have tried all those variations:
include_once('simple_html_dom.php');
require_once('simple_html_dom.php');
include('simple_html_dom.php');
require('simple_html_dom.php');
file_get_html() returns an object, not a string. Attempting to concatenate a string to an object will call the object's _toString() method if it exists, and the operation returns a string. Strings do not have a find() method.
If you want to do as you have described read the file contents and concatenate the extra string first:
$content = file_get_contents('someFile.html');
$content .= "someString";
$domObject = str_get_html($content);
Alternatively, read the file with file_get_html() and manipulate it with the DOM API.
$doo is not a string! It's an object, an instance of Simple HTML DOM. You can't call -> methods on strings, only on objects. You cannot treat this object like a string. Trying to concatenate something to it makes no sense. $abd in your code is the result of an object concatenated with a string; this either results in a string or an error, depending in the details of the object. What it certainly does not do is result in a usable object, so you certainly can't do $abd->find().
If you want to modify the content of the page, do it using the DOM API which the object gives you.
I am getting some variable where is XML file, I can't edit it or do anything with it.
So what I do:
$xml = $client->get_details('WF0GXXGBBG7P857BB');
$xml = simplexml_load_string($xml);
//print_r($xml);
$vin = $xml->vin;
print_r($vin);
If I uncomment print_r($xml) it just prints out whole xml and works cool (output is http://pastebin.com/w5VVysZU), but if I use second part with print_r($vin) it just displays just SimpleXMLElement Object ( ),
Any idea what can I do? How can I fix this? I've tried like 20 tutorials and always get no output or error with using nonobject something.
EDIT 1:
I need it to display one specific thing from this XML, in example it's VIN, so from this big amount I want script to find where is [vin] => WF0GXXGBBG7P857BB and echo WF0GXXGBBG7P857BB
EDIT 2:
My XML: http://pastebin.com/1KLB5Ba0
SimpleXML elements can and have to be type casted to strings if you want to display them that way. Otherwise, as you seen, it just tells you that it is an object.
$vin = (string) $xml->vin;
// OR
print_r((string) $vin);
I am rather new to development, but a long time sys-admin. I am trying to retrieve a specific value from the following XML feed. (Its oBix).
http://80.68.58.47:900/obix/config/Drivers/NiagaraNetwork/OSS_Tridium_Demo/points/kW_Sys/
I am trying to return the "Out" value within the 'Real' element (If element is the right name for it). The value is a number next to val=.
I have spent a fair few hours trying to work out how to isolate the 'val' attribute and return the number but I can't seem to do it. I can isolate the 'real' element but can't go any further in to get the actual number value.
Could someone show me how this is done using PHP SimpleXML so I can try to get my head round it?
This is my code so far:
<?php
$url = "http://80.68.58.47:900/obix/config/Drivers/NiagaraNetwork/OSS_Tridium_Demo/points/kW_Sys/";
$xml = simplexml_load_file($url);
$elementselection = $xml->real[0];
$outputvalue = $elementselection;
print_r ($outputvalue);
?>
Many Thanks!
Tom
If you just want the value from the first 'real' element you can use the following:
$url = "http://80.68.58.47:900/obix/config/Drivers/NiagaraNetwork/OSS_Tridium_Demo/points/kW_Sys/";
$xml = simplexml_load_file($url);
$value = (string) $xml->real[0]['val'];
var_dump($value);
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)