Home > Net >  i can't read and parse xml file in php
i can't read and parse xml file in php

Time:07-07

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>

CodePudding user response:

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. A random item in this array might look like this:

    {
        "tag": "XS:ELEMENT",
        "type": "complete",
        "level": 9,
        "attributes": {
            "NAME": "ARABIC_NAME",
            "TYPE": "xs:string",
            "MINOCCURS": "0"
        }
    },

You are looking to loop the <Ticker> tags.

<Ticker diffgr:id="Ticker209" msdata:rowOrder="208">
    <SYMBOL>AMPI.CA</SYMBOL>
    <ARABIC_NAME>المؤشر للبرمجيات ونشر المعلومات</ARABIC_NAME>
    <CLOSE>2.37</CLOSE>
    <OPEN>2.37</OPEN>
    <ENGLISH_NAME>Al Moasher for Programming and Information Dissemination</ENGLISH_NAME>
</Ticker>

It's equivalent array nodes are (remember this is flat array):

 {
        "tag": "TICKER",
        "type": "open",
        "level": 4,
        "attributes": {
            "DIFFGR:ID": "Ticker209",
            "MSDATA:ROWORDER": "208"
        },
        "value": "\n        "
    },
    {
        "tag": "SYMBOL",
        "type": "complete",
        "level": 5,
        "value": "AMPI.CA"
    },
    {
        "tag": "TICKER",
        "value": "\n        ",
        "type": "cdata",
        "level": 4
    },
    {
        "tag": "ARABIC_NAME",
        "type": "complete",
        "level": 5,
        "value": "المؤشر للبرمجيات ونشر المعلومات"
    },
    {
        "tag": "TICKER",
        "value": "\n        ",
        "type": "cdata",
        "level": 4
    },
    {
        "tag": "CLOSE",
        "type": "complete",
        "level": 5,
        "value": "2.37"
    },
    {
        "tag": "TICKER",
        "value": "\n        ",
        "type": "cdata",
        "level": 4
    },
    {
        "tag": "OPEN",
        "type": "complete",
        "level": 5,
        "value": "2.37"
    },
    {
        "tag": "TICKER",
        "value": "\n        ",
        "type": "cdata",
        "level": 4
    },
    {
        "tag": "ENGLISH_NAME",
        "type": "complete",
        "level": 5,
        "value": "Al Moasher for Programming and Information Dissemination"
    },
    {
        "tag": "TICKER",
        "value": "\n      ",
        "type": "cdata",
        "level": 4
    },
    {
        "tag": "TICKER",
        "type": "close",
        "level": 4
    },
    {
        "tag": "TICKER",
        "value": "\n      ",
        "type": "cdata",
        "level": 3
    },

So there's some work to do here, but I think all the data is there.

  • Related