php simplxml - Trying to get property of non-object in [duplicate] - php

This question already has an answer here:
Trying to get property of non-object SimpleXML?
(1 answer)
Closed 8 years ago.
So I'm getting an error trying to access player name in an XML File using the code. I'm not sure if i'm accessing the information right is there something I'm doing wrong?
<?php
$xml = simplexml_load_file('trueskill.bcn');
$playername = $xml->SK92->Players[0]->Name;
echo $playername;
?>
Using the XML File
<?xml version="1.0" encoding="utf-8"?>
<SK92>
<Settings Multiplier="200" Decay="0" />
<Players>
<Player Name="Mark" Team="" Invisible="false" Characters="" Alts="JFG;Mark;SEBA" />
<Player Name="Antonio" Team="" Invisible="false" Characters="" Alts="Proxy;Toni" />
</Players>
</SK92>

echo $xml->Players->Player['Name'];
should do the trick.
To have it in variables:
$playername = $xml->Players->Player['Name'];
echo $playername;
SIDENOTE:
As I suppose you want to access all the players, you can do this by:
foreach ($xml->Players->Player as $play) {
echo $play['Name'] . "<br>\n";
}

Related

Why does SimpleXML XPath return an empty array? [duplicate]

This question already has an answer here:
PHP simplexml: why does xpath stop working?
(1 answer)
Closed 8 years ago.
I've the following XML-File:
<SyncCustomerPartyMaster xmlns="http://schema.infor.com/InforOAGIS/2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:schemaLocation="http://schema.infor.com/InforOAGIS/2 http://schema.infor.com/2.9.1/InforOAGIS/BODs/Developer/SyncCustomerPartyMaster.xsd" releaseID="9.2" versionID="2.9.1">
<ApplicationArea>
<Sender>
<LogicalID>lid://infor.ln.dach_nausveln1_200</LogicalID>
<ComponentID>erp</ComponentID>
<ConfirmationCode>OnError</ConfirmationCode>
</Sender>
<CreationDateTime>2014-10-09T13:47:48Z</CreationDateTime>
</ApplicationArea>
<DataArea>
<Sync>
<TenantID>infor</TenantID>
<AccountingEntityID>200</AccountingEntityID>
<LocationID/>
<ActionCriteria>
<ActionExpression actionCode="Add"/>
</ActionCriteria>
</Sync>
<CustomerPartyMaster>
<PartyIDs>
<ID accountingEntity="200" lid="lid://infor.ln.dach_nausveln1_200" variationID="108">10100</ID>
<DisplayID>10100</DisplayID>
</PartyIDs>
</CustomerPartyMaster>
</DataArea>
</SyncCustomerPartyMaster>
And I'm trying to access the DisplayId via XPath: '/SyncCustomerPartyMaster/DataArea/CustomerPartyMaster/PartyIDs/DisplayID'
<?php
$xml = simplexml_load_file("example.xml");
$displayId = $xml->xpath('/SyncCustomerPartyMaster/DataArea/CustomerPartyMaster/PartyIDs/DisplayID');
print_r($xml);
echo '<br />';
echo '<br />';
print_r($displayId);
?>
But print_r($display_id) just returns an empty array. I fiddled around a bit with registerXPathNamespace]1 which did not do the trick - can anyone please point me into the right direction?
xmlns="http://schema.infor.com/InforOAGIS/2"
This declares a default namespace which you do need to register via registerXPathNamespace() and address the elements using the prefix registered.
Example:
$xml = simplexml_load_file("example.xml");
$xml->registerXPathNamespace('ns', "http://schema.infor.com/InforOAGIS/2");
$displayIds = $xml->xpath('/ns:SyncCustomerPartyMaster/ns:DataArea/ns:CustomerPartyMaster/ns:PartyIDs/ns:DisplayID');
foreach ($displayIds as $displayId) {
echo $displayId;
}
Output:
10100

Parsing XML with namespace information [duplicate]

This question already has answers here:
SimpleXML SOAP response Namespace issues
(2 answers)
Closed 8 years ago.
I found lot of solutions for this problem, but my code won't work!
XML INFO:
<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:resultado xmlns:ns2="http://webservice.consulta.spcjava.spcbrasil.org/" data="2014-06-03T11:37:32.001-03:00" restricao="false">
<protocolo digito="2" numero="1204248496" />
.... other XML info
MY CODE:
$s = '<?xml version="1.0" encoding="UTF-8"?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:resultado xmlns:ns2="http://webservice.consulta.spcjava.spcbrasil.org/" data="2014-06-03T11:37:32.001-03:00" restricao="false"><protocolo digito="2" numero="1204248496" /> ...
$xml = simplexml_load_string($s);
$x2 = $xml->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->children('http://webservice.consulta.spcjava.spcbrasil.org/')->resultado->protocolo->digito;
print_r($x2);
var_dump(count($x2));
Returns null and 0 for the object count.
I've been following this tutorial: http://amigotechnotes.wordpress.com/2013/11/16/parse-xml-with-namespace-by-simplexml-in-php/
I don't get where my example differs from his example. :/
Can anyone help me with this issue, please?
Finally made it work using another syntax.
$xml = simplexml_load_string($resultadoDocumento[0]["cdo_xml"]);
$resultadoSPC = $xml->children('S', TRUE)->Body->children('ns2', TRUE)->children();
accessing nodes by:
<?php foreach ($resultadoSPC->consumidor->children()->{"consumidor-pessoa-fisica"} as $consumidorElement) : ?>
<?php echo $consumidorElement->attributes()->{"nome"}; ?>

simplexml parsing with php [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 8 years ago.
I am trying to print out the xml child "pizza" on a php page. Though it is not working, here is my code for parsing the xml file.
<?
$dom = simplexml_load_file("menu.xml");
foreach ($dom->menu->categories->pizzas->pizza as $pizza)
{
echo $pizza;
}
?>
This is the xml file
<menu>
<categories>
<pizzas>
<pizza>Cheese Pizza</pizza>
<pizza>Beef PIzza</pizza>
<pizza>Chickens Wings Pizza</pizza>
</pizzas>
</categories>
</menu>
I would just like to print out the different kinds of pizzas on my php page. I would like to display a menu on the page.
I get an error of "Trying to get property of non-object in /Applications/XAMPP/xamppfiles/htdocs/home.php on line 15"
Please Help!
Thanks
Remove the top level element menu from that loop and use
foreach ($dom->categories->pizzas->pizza as $pizza)
{
echo $pizza;
}
If you do print_r($dom); you will get a proof of that structure.
<?php
$dom = simplexml_load_file("menu.xml");
foreach($dom->categories->pizzas->pizza as $pizza) {
echo $pizza;
} // Try This
?>

parsing xml file in php [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 9 years ago.
I have an xml file which consist of name of the country and its code.
<country>
<name>ALBANIA</name>
<code>AL</code>
</country>
<country>
<name>ALGERIA</name>
<code>DZ</code>
</country>
<country>
<name>AMERICAN SAMOA</name>
<code>AS</code>
</country>
now I am using following php code to store them in array and printing them(country.xml file is in the same folder as this php code.
$countries = array();
$file = new SimpleXMLElement(__DIR__ . '/country.xml', null, true);
foreach ($file->country as $country) {
$name = trim($country['name']);
$code = trim(strtoupper($country['code']));
$countries[$code] = $name;
echo $code;
}
but this php code shows blank page. Can anyone guide me where I am making mistake and help me to correct it or give some better method to parse xml file.
The simplexml_load_file() in PHP will do the job.
<?php
$xml = simplexml_load_file('country.xml');
$i=0;
$countryName=array();
$countryCode=array();
foreach($xml as $k=>$v)
{
$countryName[$i] = (string) $xml->country[$i]->name;
$countryCode[$i] = (string) $xml->country[$i]->code;
$i++;
}
print_r($countryName);
print_r($countryCode);
?>

Parse SOAP response with namespaces in PHP [duplicate]

This question already has answers here:
parse an XML with SimpleXML which has multiple namespaces [duplicate]
(5 answers)
Closed 8 years ago.
Any recommendations how to parse this SOAP response and obtain the value of name for the report_type? Notice there are two instances of name; one under report_type and the other under severity.
Here is the SOAP response
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns1:getIRResponse xmlns:ns1="http://ws.icontent.idefense.com/V3/2">
<ns1:return xsi:type="ns1:IRResponse" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ns1:report_type>
<ns1:id>0</ns1:id>
<ns1:name>Original Vulnerability</ns1:name>
</ns1:report_type>
<ns1:severity>
<ns1:id>0</ns1:id>
<ns1:name>HIGH</ns1:name>
</ns1:severity>
</ns:return>
</ns1:getIRResponse>
</soapenv:Body>
</soapenv:Envelope>
Here is the PHP code I'm using:
<?php
$xml = simplexml_load_string($response);
$xml->registerXPathNamespace('ns1', 'http://ws.icontent.idefense.com/V3/2');
foreach ($xml->xpath('//ns1:report_type/name') as $item)
{
echo 'Name: '.$item,'<br>';
}
?>
The PHP code doesn't echo anything. When I use ($xml->xpath('//ns1:name') as $item) it returns both names (Original Vulnerability and HIGH).
I know I'm missing something stupid. Can you help please? Thank you in advance!
Firstly, I've corrected this element
</ns:return>
and changed it to
</ns1:return>
I seem to get the result you're after by duplicating the namespace prefix in both xpath segments
$xml = simplexml_load_string($response);
$xml->registerXPathNamespace('ns1','http://ws.icontent.idefense.com/V3/2');
foreach ($xml->xpath('//ns1:report_type/ns1:name') as $item)
{
echo 'Name: '.$item,'<br>';
}
output
Name: Original Vulnerability

Categories