Reputation: 23
i have a xml file and I am trying to access the data in a xml file but it return an empty array.
i have a xml file and I am trying to access the data in a xml file but it return an empty array.
Could the problem be in the file itself?
I have tried to use Basic SimpleXML usage this is my php code i used:
<?php
libxml_use_internal_errors(TRUE);
$xml = file_get_contents("https://egytech4uu.herokuapp.com/data.xml");
$XML = simplexml_load_string($xml, "SimpleXMLElement", LIBXML_NOCDATA);
$json = json_encode($XML);
$arr = json_decode($json,TRUE);
print_r($arr);
?>
<?xml version="1.0" encoding="UTF-8"?>
<DataSet xmlns="http://tempuri.org/Prices_Feed/Service1">
<xs:schema id="Ticker" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="Ticker" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Time">
<xs:complexType>
<xs:sequence>
<xs:element name="TIME" type="xs:dateTime" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Ticker">
<xs:complexType>
<xs:sequence>
<xs:element name="SYMBOL" type="xs:string" minOccurs="0"/>
<xs:element name="ARABIC_NAME" type="xs:string" minOccurs="0"/>
<xs:element name="CLOSE" type="xs:decimal" minOccurs="0"/>
<xs:element name="OPEN" type="xs:decimal" minOccurs="0"/>
<xs:element name="ENGLISH_NAME" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<Ticker xmlns="">
<Time diffgr:id="Time1" msdata:rowOrder="0">
<TIME>2022-03-28T14:29:56+02:00</TIME>
</Time>
<Ticker diffgr:id="Ticker1" msdata:rowOrder="0">
<SYMBOL>OFH.CA</SYMBOL>
<ARABIC_NAME>اوراسكوم المالية القابضة</ARABIC_NAME>
<CLOSE>0.1820</CLOSE>
<OPEN>0.19</OPEN>
<ENGLISH_NAME>Orascom Financial Holding</ENGLISH_NAME>
</Ticker>
<Ticker diffgr:id="Ticker2" msdata:rowOrder="1">
<SYMBOL>AMOC.CA</SYMBOL>
<ARABIC_NAME>الاسكندرية للزيوت المعدنية</ARABIC_NAME>
<CLOSE>4.08</CLOSE>
<OPEN>4.33</OPEN>
<ENGLISH_NAME>Alexandria Mineral Oils Company</ENGLISH_NAME>
</Ticker>
<Ticker diffgr:id="Ticker3" msdata:rowOrder="2">
<SYMBOL>OIH.CA</SYMBOL>
<ARABIC_NAME>اوراسكوم للاستثمار القابضة</ARABIC_NAME>
<CLOSE>0.2250</CLOSE>
<OPEN>0.2280</OPEN>
<ENGLISH_NAME>Orascom Investment Holding</ENGLISH_NAME>
</Ticker>
<Ticker diffgr:id="Ticker4" msdata:rowOrder="3">
<SYMBOL>EDBM.CA</SYMBOL>
<ARABIC_NAME>المصرية لتطوير صناعة البناء (ليفت سلاب مصر )</ARABIC_NAME>
<CLOSE>0.2690</CLOSE>
<OPEN>0.2780</OPEN>
<ENGLISH_NAME>Egyptian for Developing Building Materials</ENGLISH_NAME>
</Ticker>
<Ticker diffgr:id="Ticker5" msdata:rowOrder="4">
<SYMBOL>MTIE.CA</SYMBOL>
<ARABIC_NAME>ام.ام جروب للصناعة والتجارة العالمية</ARABIC_NAME>
<CLOSE>4.13</CLOSE>
<OPEN>4.46</OPEN>
<ENGLISH_NAME>MM Group For Industry And International Trade</ENGLISH_NAME>
</Ticker>
<Ticker diffgr:id="Ticker6" msdata:rowOrder="5">
<SYMBOL>UNIP.CA</SYMBOL>
<ARABIC_NAME>يونيفرسال لصناعة مواد التعبئة و التغليف و الورق - يونيباك</ARABIC_NAME>
<CLOSE>0.4280</CLOSE>
<OPEN>0.4210</OPEN>
<ENGLISH_NAME>Universal For Paper and Packaging Materials (Unipack</ENGLISH_NAME>
</Ticker>
</Ticker>
</diffgr:diffgram>
</DataSet>
Upvotes: 1
Views: 1083
Reputation: 97718
One way to think about SimpleXML is that it doesn't create objects containing the XML it's parsed, it just gives an API for accessing data inside that XML. So to work with it, you need to understand the structure of the XML, and decide what data you want to get out of it.
In this case, the xmlns
attributes represent XML namespaces, so you need to understand how to work with those, which is discussed in detail here: Reference - How do I handle Namespaces (Tags and Attributes with a Colon in their Name) in SimpleXML?.
You haven't actually said what data you want to get out, so I'll use as an example getting the SYMBOLS from the inner list of Ticker elements. To get to those, you need to traverse through:
<diffgr:diffgram>
element, which is in namespace urn:schemas-microsoft-com:xml-diffgram-v1
as indicated by its xmlns:diffgr
attribute<Ticker>
element, which is in a namespace with an empty URI (xmlns=""
)<Ticker>
elements, which we want to loop over<SYMBOL>
element in each one, which we want to extract the string content of$sx = simplexml_load_string($xml); // Note: no additional options needed here
// Switch to the namespace given by xmlns:diffgr=""...", and select the "diffgram" element
$diffgram = $sx->children('urn:schemas-microsoft-com:xml-diffgram-v1')->diffgram;
// Switch to the namespace with an empty URI, because the elements have xmlns=""
$emptyNamespaceChildren = $diffgram->children("");
// Select the outer Ticker element
$outerTicker = $emptyNamespaceChildren->Ticker;
// Loop over the inner Ticker elements
$symbols = [];
foreach ( $outerTicker->Ticker as $ticker ) {
// Get some data out, in this case the SYMBOL of each Ticker
// Using (string) gives us the content of the element, rather than an object
$symbols[] = (string)$ticker->SYMBOL;
}
Note that all the extra variables here are just for readability, and in practice you might not write it so verbosely. At the other extreme, you could put it all on one line, going through all the levels until you get to the part where you want to loop, like this:
$sx = simplexml_load_string($xml);
$symbols = [];
foreach ( $sx->children('urn:schemas-microsoft-com:xml-diffgram-v1')->diffgram->children("")->Ticker->Ticker as $ticker ) {
$symbols[] = (string)$ticker->SYMBOL;
}
Upvotes: 3
Reputation: 19485
Here's one for you:
<?php
$xmldata = file_get_contents("https://egytech4uu.herokuapp.com/data.xml");
$xmlparser = xml_parser_create();
$arr = [];
xml_parse_into_struct($xmlparser, $xmldata, $arr);
xml_parser_free($xmlparser);
print_r($arr);
So what's going there? It returns a flat array, but for each node at least we can see its level. As mentioned in the comments, this is not the best way to parse XML, only a last resort kind of thing.
Upvotes: -1