To add to what others have said, you can't directly put a $_GET or $_POST value into a variable then into an attribute using SimpleXML. You must first convert it to an integer.
This will NOT work
<?php
$page_id = $_GET['id'];
echo $xml->page[$page_id]
?>
You will get something like:
Notice: Trying to get property of non-object in /Applications/MAMP/htdocs/mysite/index.php on line 10
However, this WILL work and is much simpler then using (string) or other methods.
<?php
$page_id = intval($_GET['id']);
echo $xml->page[$page_id]
?>
SimpleXML
- 소개
- 설치/설정
- 예약 상수
- 예제
- SimpleXML 함수 목록
- SimpleXMLElement->addAttribute — Adds an attribute to the SimpleXML element
- SimpleXMLElement->addChild — Adds a child element to the XML node
- SimpleXMLElement->asXML — Return a well-formed XML string based on SimpleXML element
- SimpleXMLElement->attributes — Identifies an element's attributes
- SimpleXMLElement->children — Finds children of given node
- SimpleXMLElement->__construct — Creates a new SimpleXMLElement object
- SimpleXMLElement->getDocNamespaces — Returns namespaces declared in document
- SimpleXMLElement->getName — Gets the name of the XML element
- SimpleXMLElement->getNamespaces — Returns namespaces used in document
- SimpleXMLElement->registerXPathNamespace — Creates a prefix/ns context for the next XPath query
- SimpleXMLElement->xpath — Runs XPath query on XML data
- simplexml_import_dom — Get a SimpleXMLElement object from a DOM node.
- simplexml_load_file — Interprets an XML file into an object
- simplexml_load_string — Interprets a string of XML into an object
SimpleXML
oscargodson at gmail dot com
25-Jan-2009 01:05
25-Jan-2009 01:05
bxt at die-optimisten dot net
26-Nov-2008 05:47
26-Nov-2008 05:47
Addition to QLeap's post:
SimpleXML will return a reference to an object containing the node value and you can't use references in session variables as there is no feasible way to restore a reference to another variable.
This won't work too:
$val=$this->xml->node->attributes()->name;
echo $array[$val]; // will cause a warning because of the wrong index type.
You have to convert/cast to a String first:
echo $array[(string)$val];
This will work as expected, because converting will call the __toString() method. Therefor echo works too:
echo $val; // will display the name
QLeap
19-Nov-2008 12:27
19-Nov-2008 12:27
Storing SimpleXMLElement values in $_SESSION does not work. Saving the results as an object or individual elements of the object will result in the dreaded "Warning: session_start() [function.session-start]: Node no longer exists" error.
For example, this does not work:
$xml = new SimpleXMLElement($page);
$country = $xml->Response->Placemark->AddressDetails->Country->CountryNameCode;
$_SESSION['country'] = $country;
This will work:
$_SESSION['country'] = (string) $country;
gwhitescarver at yahoo dot com
11-Sep-2008 11:02
11-Sep-2008 11:02
Moving some code from a PHP 5.2.6 / Windows environment to a 5.2.0 / Linux environment, I somehow lost access to a plain text node within a SimpleXML Object. On a var_dump of $xml_node, a [0] element was shown as the string '12'. However, $xml_node[0] was evaluating NULL in 5.2.0. You can see below the code change I made, pulling my data out of the raw XML with a regular expression. Hope this is useful to someone.
//In some versions of PHP it seems we cannot access the [0] element of a SimpleXML Object. Doesn't work in 5.2.0:
//$count = $xml_node[0];
//grab the raw XML:
$count = ($xml_node->asXML());
//pull out the number between the closing and opening brace of the xml:
$count = preg_replace('/.*>(\d*)<.*/', '$1', $count);
charlie at blankcanvasstudios dot com
14-Aug-2008 09:21
14-Aug-2008 09:21
Optimizing aalaap at gmail dot com's php
<?php
function is_rss($feedxml) {
@$feed = simplexml_load_string($feedxml);
return ($feed->channel->item)?true:false;
}
function is_atom($feedxml) {
@$feed = new SimpleXMLElement($feedxml);
($feed->entry):true:false;
}
?>
aalaap at gmail dot com
30-Jun-2008 04:58
30-Jun-2008 04:58
Here are two quick and dirty functions that use SimpleXML to detect if a feed xml is RSS or ATOM:
<?php
function is_rss($feedxml) {
@$feed = new SimpleXMLElement($feedxml);
if ($feed->channel->item) {
return true;
} else {
return false;
}
}
function is_atom($feedxml) {
@$feed = new SimpleXMLElement($feedxml);
if ($feed->entry) {
return true;
} else {
return false;
}
}
?>
The functions take in the full text feed (retrieved via cURL, for example) and return a true or a false based on the result.
