K
K
KomarovMaksim2014-06-25 13:17:59
PHP
KomarovMaksim, 2014-06-25 13:17:59

SimpleXML data parsing?

There is XML of this type

<?xml version="1.0" encoding="UTF-8"?>
<Owners>
  <!--Информация по владельцам лицевых счетов-->
  <Owner id="0000001081" name="???" email="">
    <LicAccounts>
      <LicAccount name="1301001"/>
      <LicAccount name="1301002"/>
      <LicAccount name="1301003"/>
      <LicAccount name="1301009">
        <Address>Какой-то адресс</Address>
        <Periods>
          <Period year="2014" month="04">
            <DebtOnStartMonth>0</DebtOnStartMonth>
            <Withheld>0</Withheld>
            <Payment>0</Payment>
            <DebtOnEndMonth>0</DebtOnEndMonth>
            <Services>
              <Service>
                <Name>Коллективная антенна</Name>
                <Withheld>0</Withheld>
                <Payment>0</Payment>
              </Service>
              <Service>
                <Name>Радиоточка и оповещение</Name>
                <Withheld>0</Withheld>
                <Payment>0</Payment>
              </Service>
            </Services>
          </Period>
        </Periods>
      </LicAccount>
      <LicAccount name="1301065"/>
      <LicAccount name="1301073"/>
      <LicAccount name="1301097"/>
      <LicAccount name="1301098"/>
      <LicAccount name="1301104"/>
      <LicAccount name="1301138"/>
      <LicAccount name="1301145"/>
    </LicAccounts>
  </Owner>
  <Owner id="0000000962" name="Pan-American Real Estate Investment LLC" email="">
    <LicAccounts>
      <LicAccount name="060301179">
        <Address>Какой-то адресс</Address>
        <Periods>
          <Period year="2014" month="04">
            <DebtOnStartMonth>0</DebtOnStartMonth>
            <Withheld>0</Withheld>
            <Payment>99 021,17</Payment>
            <DebtOnEndMonth>0</DebtOnEndMonth>
            <Services>
              <Service>
                <Name>Техническое обслуживание</Name>
                <Withheld>0</Withheld>
                <Payment>24 014,21</Payment>
              </Service>
              <Service>
                <Name>Отопление</Name>
                <Withheld>0</Withheld>
                <Payment>10 527,2</Payment>
              </Service>
              <Service>
                <Name>Охрана объекта</Name>
                <Withheld>0</Withheld>
                <Payment>628,17</Payment>
              </Service>
              <Service>
                <Name>Электроэнергия офисы</Name>
                <Withheld>0</Withheld>
                <Payment>56 420,13</Payment>
              </Service>
              <Service>
                <Name>Холодное водоснабжение</Name>
                <Withheld>0</Withheld>
                <Payment>745,45</Payment>
              </Service>
              <Service>
                <Name>Горячее водоснабжение</Name>
                <Withheld>0</Withheld>
                <Payment>1 311,46</Payment>
              </Service>
              <Service>
                <Name>Водоотведение</Name>
                <Withheld>0</Withheld>
                <Payment>751,8</Payment>
              </Service>
              <Service>
                <Name>Электроэнергия офисы МОП</Name>
                <Withheld>0</Withheld>
                <Payment>1 016,76</Payment>
              </Service>
              <Service>
                <Name>ТО перерасчет янв-фев 14</Name>
                <Withheld>0</Withheld>
                <Payment>2 326,71</Payment>
              </Service>
              <Service>
                <Name>Охр.объек перер янв-фев'14</Name>
                <Withheld>0</Withheld>
                <Payment>1 256,33</Payment>
              </Service>
            </Services>
          </Period>
        </Periods>
      </LicAccount>
    </LicAccounts>
  </Owner>
       <Owner>
         ...
      </Owner>
</Owners>

It is necessary to go through all the data and overtake them in MySQL.
$one_c=  simplexml_load_file(ROOT_DIR.'/LicAcc.xml');
foreach ($one_c->Owners->xpath('//Owner') as $owners)
{
  
  foreach($owners->LicAccounts->xpath('//LicAccount') as $licaccounts)
  {
    echo $licaccounts->attributes()->name."\r\n"; 
    ob_flush(); 
    flush();
  }
}

The code is debugging so far. The problem is that in the second loop it loops through all the "LicAccount" elements each time, not the current Parent "Owner". How to be?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey, 2014-06-25
@KomarovMaksim

I fixed everything ideone.com/8qSpfW But why you need xpath is not clear, but if you suddenly need it, it's better like this ideone.com/Y2fJEk
Axis // looks anywhere, regardless of the current element.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question