I have a XML which I parse with php simpleXML.
The XML:
<GetOneGetAll DateTimeSystem="28-06-2011 17:19:29" RetCode="200" RetVal="1" RetMsg="User ok.">
<User Id="bc5cb4cf-19a6-4504-8e1a-f72dd97bcc66" ReferedConfirmedUsers="0" TotalRecomendations="0" DistinctRecomendations="0">
<Name>Name</Name>
<Surname>Surname</Surname>
<Gender>F</Gender>
<Email>email#email.com</Email>
<RefererCode>59286904</RefererCode>
<CustomPhotoMessage HasCustomPhoto="0" HasCustomMessage="0"/>
<ReferedConfirmedUsersList/>
</User>
</GetOneGetAll>
When I print_r the var using simpleXML I get:
SimpleXMLElement Object
(
[#attributes] => Array
(
[DateTimeSystem] => 28-06-2011 17:22:52
[RetCode] => 200
[RetVal] => 1
[RetMsg] => Login ok.
)
[User] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Id] => bc5cb4cf-19a6-4504-8e1a-f72dd97bcc66
[ReferedConfirmedUsers] => 0
[TotalRecomendations] => 0
[DistinctRecomendations] => 0
)
[Name] => SimpleXMLElement Object
(
)
[Surname] => SimpleXMLElement Object
(
)
[Gender] => SimpleXMLElement Object
(
)
[Email] => SimpleXMLElement Object
(
)
[RefererCode] => SimpleXMLElement Object
(
)
[CustomPhotoMessage] => SimpleXMLElement Object
(
[#attributes] => Array
(
[HasCustomPhoto] => 0
[HasCustomMessage] => 0
)
)
[ReferedConfirmedUsersList] => SimpleXMLElement Object
(
)
)
)
Where is the data of Surname, Name, Email, Gender, etc.?
This is just a guess, but if you have xdebug installed, then the default recursion level of var_dump output is 3. This setting is xdebug.var_display_max_depth
You are using print_r, but some similar recursive limit could be being reached.
Related
I receive this warning when I try to get data from a call, I've tried with some solution for the same topic in stackoverflow but it doesn't work.
[2] simplexml_load_string(): Entity: line 4: parser error : Start tag expected, '<' not found
simplexml_load_string(): ^
the php code is as follow
$api = new MktApi();
$arr = array();
$xlm = $api->handleResponse($api->getBrands());
print_r($xlm);
$xml = simplexml_load_string($xlm);
and the print I have is like this
SimpleXMLElement Object ( [Head] => SimpleXMLElement Object ( [RequestId] => SimpleXMLElement Object ( ) [RequestAction] => GetBrands [ResponseType] => Brands [Timestamp] => 2017-05-04T16:29:44-0500 ) [Body] => SimpleXMLElement Object ( [Brands] => SimpleXMLElement Object ( [Brand] => Array ( [0] => SimpleXMLElement Object ( [BrandId] => 22912 [Name] => DC Comics [GlobalIdentifier] => 101020216193 ) [1] => SimpleXMLElement Object ( [BrandId] => 23324 [Name] => MIOS [GlobalIdentifier] => 101020216475 ) [2] => SimpleXMLElement Object ( [BrandId] => 32298 [Name] => 988 [GlobalIdentifier] => SimpleXMLElement Object ( ) ) [3] => SimpleXMLElement Object ( [BrandId] => 30015 [Name] => About Time [GlobalIdentifier] => SimpleXMLElement Object ( ) )
it's look like correct XML, I've tried to add the xml header but I still have the same issue.
It looks like $api->handleResponse(...) returns a SimpleXMLElement object. You don't need to parse it again.
I'm new to woking with XML with PHP. I have a fairly complex XML structure and am using simplexml in laravel and am having trouble accessing all the elements I need to get. I am able to loop through the large XML file but simpleXML is returning two objects per record and I only seem to be able to access the elements in 'header', the first object returned...
here is part of the xml object
SimpleXMLElement Object
(
[identifier] => RCM0635
[datestamp] => 2015-06-09
)
SimpleXMLElement Object
(
[lidoWrap] => SimpleXMLElement Object
(
[lido] => SimpleXMLElement Object
(
[lidoRecID] => RCM:1748
[descriptiveMetadata] => SimpleXMLElement Object
(
[objectClassificationWrap] => SimpleXMLElement Object
(
[objectWorkTypeWrap] => SimpleXMLElement Object
(
[objectWorkType] => SimpleXMLElement Object
(
[term] => musical instruments
)
)
[classificationWrap] => SimpleXMLElement Object
(
[classification] => Array
(
[0] => SimpleXMLElement Object
(
[term] => Cornet
)
[1] => SimpleXMLElement Object
(
[conceptID] => SimpleXMLElement Object
(
[#attributes] => Array
(
[type] => SH_Class
)
)
)
)
)
)
With the code below I can get the elements in the header but I can't figure out how to get the other elements?
$streamer = \Prewk\XmlStringStreamer::createStringWalkerParser(public_path().'/xml/many_mimo_records.xml');
while ($node = $streamer->getNode()) {
$simpleXmlNode = simplexml_load_string($node);
echo (string)$simpleXmlNode->identifier;
echo (string)$simpleXmlNode->datestamp;
}
I'd be very grateful for any advice...
I'm not sure if I understand You but in major:
You act on while ($node = $streamer->getNode()) loop what means that after first iteration You'll get this object:
SimpleXMLElement Object
(
[identifier] => RCM0635
[datestamp] => 2015-06-09
)
so for the first time it's ok to read it like:
`
$simpleXmlNode = simplexml_load_string($node);
echo (string)$simpleXmlNode->identifier;
echo (string)$simpleXmlNode->datestamp;
`
but in the second iteration You have:
`
SimpleXMLElement Object
(
[lidoWrap] => SimpleXMLElement Object
(
[lido] => SimpleXMLElement Object
(
[lidoRecID] => RCM:1748
[descriptiveMetadata] => SimpleXMLElement Object
(
[objectClassificationWrap] => SimpleXMLElement Object
(
[objectWorkTypeWrap] => SimpleXMLElement Object
(
[objectWorkType] => SimpleXMLElement Object
(
[term] => musical instruments
)
)
[classificationWrap] => SimpleXMLElement Object
(
[classification] => Array
(
[0] => SimpleXMLElement Object
(
[term] => Cornet
)
[1] => SimpleXMLElement Object
(
[conceptID] => SimpleXMLElement Object
(
[#attributes] => Array
(
[type] => SH_Class
)
)
)
)
)
)
`
so the code inside while is wrong.
i sugest to try something like this:
`
while ($node = $streamer->getNode()) {
$simpleXmlNode = simplexml_load_string($node);
if (!empty($simpleXmlNode->identifier))
echo (string)$simpleXmlNode->identifier;
if (!empty($simpleXmlNode->datestamp))
echo (string)$simpleXmlNode->datestamp;
if (!empty($simpleXmlNode->lidoWrap)) {
$lido = $simpleXmlNode->lidoWrap->lido;
echo (string)$lido->lidoRecID;
// and so on as the recursive XML node objects
}
}
`
I have seen so many example at stackoverflow to sort multidimensional array of object like
Sort array of objects
Sort array of objects by object fields
But none them could help me to sort my multidimensional array of objects given below
SimpleXMLElement Object
(
[request] => SimpleXMLElement Object
(
[address] => test
[citystatezip] => New York
)
[message] => SimpleXMLElement Object
(
[text] => Request successfully processed
[code] => 0
)
[response] => SimpleXMLElement Object
(
[results] => SimpleXMLElement Object
(
[result] => Array
(
[0] => SimpleXMLElement Object
(
[zpid] => 27965224
[links] => SimpleXMLElement Object
(
[homedetails] => test
[graphsanddata] =>test
[mapthishome] => test
[comparables] => test
)
[address] => SimpleXMLElement Object
(
[street] => test
[zipcode] => test
[city] => test
[state] => NY
[latitude] => 29.802114
[longitude] => -95.504244
)
[zestimate] => SimpleXMLElement Object
(
[amount] => 342911
[last-updated] => 11/27/2014
[oneWeekChange] => SimpleXMLElement Object
(
[#attributes] => Array
(
[deprecated] => true
)
)
[valueChange] => 5766
[valuationRange] => SimpleXMLElement Object
(
[low] => 312049
[high] => 373773
)
[percentile] => 0
)
[rentzestimate] => SimpleXMLElement Object
(
[amount] => 5177
[last-updated] => 11/24/2014
[oneWeekChange] => SimpleXMLElement Object
(
[#attributes] => Array
(
[deprecated] => true
)
)
[valueChange] => 370
[valuationRange] => SimpleXMLElement Object
(
[low] => 3417
[high] => 7041
)
)
[localRealEstate] => SimpleXMLElement Object
(
[region] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => 271582
[type] => neighborhood
[name] => test
)
[links] => SimpleXMLElement Object
(
[overview] => test
[forSaleByOwner] => test
[forSale] => test
)
)
)
)
[1] => SimpleXMLElement Object
[2] => SimpleXMLElement Object
[3] => SimpleXMLElement Object
..............................
..............................
)
)
)
)
I need to sort the above array on the basis of key amount in descending order. But the problem is amount key exist under two different parent keys "zestimate" and "rentzestimate".
i have tried the following function but it did not work:
public function my_comparison($a, $b) {
if ($a->amount == $b->amount) {
return 0;
}
return ($a->amount < $b->amount) ? -1 : 1;
}
Any help?
Thanks in advance
response->results->result is an array of SimpleXMLElement objects. You want to sort the array based on the inner zestimate->amount property of the element in descending order.
You have to write a comparison function that accepts SimpleXMLElement objects and, because you want a descending order, returns 1 if the zestimate->amount property of the first object is less than that of the second, -1 if it's greater and 0 if it's equal:
public function my_comparison(SimpleXMLElement $a, SimpleXMLElement $b) {
if ($a->zestimate->amount == $b->zestimate->amount) {
return 0;
}
return ($a->zestimate->amount < $b->zestimate->amount) ? 1 : -1; // note the signs
}
I am trying to parse this XML file: http://static.nvd.nist.gov/feeds/xml/cve/nvdcve-2.0-modified.xml
<?xml version='1.0' encoding='UTF-8'?>
<nvd xmlns:cvss="http://scap.nist.gov/schema/cvss-v2/0.2" xmlns="http://scap.nist.gov/schema/feed/vulnerability/2.0" xmlns:vuln="http://scap.nist.gov/schema/vulnerability/0.4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:scap-core="http://scap.nist.gov/schema/scap-core/0.1" xmlns:cpe-lang="http://cpe.mitre.org/language/2.0" xmlns:patch="http://scap.nist.gov/schema/patch/0.1" nvd_xml_version="2.0" pub_date="2013-07-11T12:00:45" xsi:schemaLocation="http://scap.nist.gov/schema/patch/0.1 http://nvd.nist.gov/schema/patch_0.1.xsd http://scap.nist.gov/schema/scap-core/0.1 http://nvd.nist.gov/schema/scap-core_0.1.xsd http://scap.nist.gov/schema/feed/vulnerability/2.0 http://nvd.nist.gov/schema/nvd-cve-feed_2.0.xsd">
<entry id="CVE-2000-0851">
<vuln:vulnerable-configuration id="http://nvd.nist.gov/">
<cpe-lang:logical-test negate="false" operator="OR">
<cpe-lang:fact-ref name="cpe:/o:microsoft:windows_2000"/>
</cpe-lang:logical-test>
</vuln:vulnerable-configuration>
<vuln:vulnerable-software-list>
<vuln:product>cpe:/o:microsoft:windows_2000</vuln:product>
</vuln:vulnerable-software-list>
<vuln:cve-id>CVE-2000-0851</vuln:cve-id>
<vuln:published-datetime>2000-11-14T00:00:00.000-05:00</vuln:published-datetime>
<vuln:last-modified-datetime>2013-07-06T00:11:34.357-04:00</vuln:last-modified-datetime>
<vuln:cvss>
<cvss:base_metrics upgraded-from-version="1.0">
<cvss:score>4.6</cvss:score>
<cvss:access-vector>LOCAL</cvss:access-vector>
<cvss:access-complexity>LOW</cvss:access-complexity>
<cvss:authentication>NONE</cvss:authentication>
<cvss:confidentiality-impact>PARTIAL</cvss:confidentiality-impact>
<cvss:integrity-impact>PARTIAL</cvss:integrity-impact>
<cvss:availability-impact>PARTIAL</cvss:availability-impact>
<cvss:source>http://nvd.nist.gov</cvss:source>
<cvss:generated-on-datetime>2004-01-01T00:00:00.000-05:00</cvss:generated-on-datetime>
</cvss:base_metrics>
</vuln:cvss>
<vuln:security-protection>ALLOWS_OTHER_ACCESS</vuln:security-protection>
<vuln:references xml:lang="en" reference_type="VENDOR_ADVISORY">
<vuln:source>BID</vuln:source>
<vuln:reference href="http://www.securityfocus.com/bid/1651" xml:lang="en">1651</vuln:reference>
</vuln:references>
<vuln:references xml:lang="en" reference_type="UNKNOWN">
<vuln:source>MS</vuln:source>
<vuln:reference href="http://www.microsoft.com/technet/security/bulletin/ms00-065.asp" xml:lang="en">MS00-065</vuln:reference>
</vuln:references>
<vuln:references xml:lang="en" reference_type="UNKNOWN">
<vuln:source>ATSTAKE</vuln:source>
<vuln:reference href="http://www.atstake.com/research/advisories/2000/a090700-1.txt" xml:lang="en">A090700-1</vuln:reference>
</vuln:references>
<vuln:references xml:lang="en" reference_type="UNKNOWN">
<vuln:source>XF</vuln:source>
<vuln:reference href="http://xforce.iss.net/static/5203.php" xml:lang="en">w2k-still-image-service</vuln:reference>
</vuln:references>
<vuln:summary>Buffer overflow in the Still Image Service in Windows 2000 allows local users to gain additional privileges via a long WM_USER message, aka the "Still Image Service Privilege Escalation" vulnerability.</vuln:summary>
</entry>
<entry id="CVE-2004-0685">
...
I do the following
$url = 'http://static.nvd.nist.gov/feeds/xml/cve/nvdcve-2.0-modified.xml';
$source = file_get_contents($url);
$xml = new SimpleXMLElement($source);
If I echo the $source, then I see that the whole XML file has been loaded, but if I print_r $xml, only the id's are echo-ed:
SimpleXMLElement Object
(
[#attributes] => Array
(
[nvd_xml_version] => 2.0
[pub_date] => 2013-07-11T12:00:45
)
[entry] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => CVE-2000-0851
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => CVE-2004-0685
)
)
Why am I missing all the information in the "entry" tags
Maybe this could get you started:
<?php
$url = 'http://static.nvd.nist.gov/feeds/xml/cve/nvdcve-2.0-modified.xml';
$source = file_get_contents($url);
$xml = new SimpleXMLElement($source);
$entries = $xml->entry;
foreach ($entries as $entry) {
$namespace = $entry->getNameSpaces(true);
$tmp = $entry->children($namespace['vuln']);
//print_r($namespace);
print_r($tmp);
break;
}
Outputs:
SimpleXMLElement Object
(
[vulnerable-configuration] => SimpleXMLElement Object
(
)
[vulnerable-software-list] => SimpleXMLElement Object
(
[product] => cpe:/o:microsoft:windows_2000
)
[cve-id] => CVE-2000-0851
[published-datetime] => 2000-11-14T00:00:00.000-05:00
[last-modified-datetime] => 2013-07-06T00:11:34.357-04:00
[cvss] => SimpleXMLElement Object
(
)
[security-protection] => ALLOWS_OTHER_ACCESS
[references] => Array
(
[0] => SimpleXMLElement Object
(
[source] => BID
[reference] => 1651
)
[1] => SimpleXMLElement Object
(
[source] => MS
[reference] => MS00-065
)
[2] => SimpleXMLElement Object
(
[source] => ATSTAKE
[reference] => A090700-1
)
[3] => SimpleXMLElement Object
(
[source] => XF
[reference] => w2k-still-image-service
)
)
[summary] => Buffer overflow in the Still Image Service in Windows 2000 allows local users to gain additional privileges via a long WM_USER message, aka the "Still Image Service Privilege Escalation" vulnerability.
)
You can un-comment print_r($namespace), to see what the custom namespaces include.
If I do print out $namespace, the output is:
Array
(
[] => http://scap.nist.gov/schema/feed/vulnerability/2.0
[vuln] => http://scap.nist.gov/schema/vulnerability/0.4
[cpe-lang] => http://cpe.mitre.org/language/2.0
[cvss] => http://scap.nist.gov/schema/cvss-v2/0.2
[xml] => http://www.w3.org/XML/1998/namespace
)
Then to get vulnerable-configuration's attributes, simply use ->getAttribute('name')
An example of this would be:
print_r($tmp->{"vulnerable-configuration"}->attributes());
You should place the name in {} because it includes an invalid character.
The above should print out:
SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => http://nvd.nist.gov/
)
)
If you don't know the values before hand, you can still loop through the $namespace variable:
foreach ($namespaces as $namespace) {
$tmp = $entry->children($namespace);
print_r($tmp);
}
The output of this would be:
SimpleXMLElement Object
(
)
SimpleXMLElement Object
(
[vulnerable-configuration] => SimpleXMLElement Object
(
)
[vulnerable-software-list] => SimpleXMLElement Object
(
[product] => cpe:/o:microsoft:windows_2000
)
[cve-id] => CVE-2000-0851
[published-datetime] => 2000-11-14T00:00:00.000-05:00
[last-modified-datetime] => 2013-07-06T00:11:34.357-04:00
[cvss] => SimpleXMLElement Object
(
)
[security-protection] => ALLOWS_OTHER_ACCESS
[references] => Array
(
[0] => SimpleXMLElement Object
(
[source] => BID
[reference] => 1651
)
[1] => SimpleXMLElement Object
(
[source] => MS
[reference] => MS00-065
)
[2] => SimpleXMLElement Object
(
[source] => ATSTAKE
[reference] => A090700-1
)
[3] => SimpleXMLElement Object
(
[source] => XF
[reference] => w2k-still-image-service
)
)
[summary] => Buffer overflow in the Still Image Service in Windows 2000 allows local users to gain additional privileges via a long WM_USER message, aka the "Still Image Service Privilege Escalation" vulnerability.
)
SimpleXMLElement Object
(
)
SimpleXMLElement Object
(
)
SimpleXMLElement Object
(
)
I'm trying to parse out the following response:
SimpleXMLElement Object (
[responseType] => SimpleXMLElement Object (
[inputConceptName] => aspirin [inputKindName] => % )
[groupConcepts] => SimpleXMLElement Object (
[concept] => Array (
[0] => SimpleXMLElement Object (
[conceptName] => ASPIRIN
[conceptNui] => N0000145918
[conceptKind] => DRUG_KIND )
[1] => SimpleXMLElement Object ( #
[conceptName] => Aspirin
[conceptNui] => N0000006582
[conceptKind] => INGREDIENT_KIND )
) ) )
in PHP. I have it stored as a curl string:
$xml = simplexml_load_string($data);
print_r($xml);
How do I get just the conceptNui of the first object as a PHP variable?
Take a look at the documentation, it give simple example to understand the friendly syntax to extract the data :
http://www.php.net/manual/en/simplexml.examples-basic.php
In your case, it could be :
$xml->groupConcepts->concept[0]->conceptNui
As your structure is
SimpleXMLElement Object (
[responseType] => SimpleXMLElement Object (
[inputConceptName] => aspirin
[inputKindName] => % )
[groupConcepts] => SimpleXMLElement Object (
[concept] => Array ([0] => SimpleXMLElement Object (
[conceptName] => ASPIRIN
[conceptNui] => N0000145918
[conceptKind] => DRUG_KIND )
[1] => SimpleXMLElement Object (
[conceptName] => Aspirin
[conceptNui] => N0000006582
[conceptKind] => INGREDIENT_KIND )
)
)
)
$conceptNui = $mainObj->groupConcepts->concept[0]->conceptNui;