困难的XML解析器

时间:2013-02-17 14:32:34

标签: php xml xml-parsing

我实际上在PHP中使用XML文件。我使用simpleXML作为基本的xml文件。但现在我有一个不同的xml文件和SimpleXML不是很好的解决方案。从我的xml我需要从zbozi kod počet获得。你有什么解决方案吗?

<?xml version="1.0" encoding="utf-8"?>
<DataSet xmlns="http://www.atcomp.cz/webservices">
  <xs:schema id="StavySkladu" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="StavySkladu" msdata:IsDataSet="true" msdata:Locale="cs-CZ">
      <xs:complexType>
        <xs:choice maxOccurs="unbounded">
          <xs:element name="Zbozi">
            <xs:complexType>
              <xs:attribute name="kod" type="xs:string" />
              <xs:attribute name="pocet" type="xs:int" />
            </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">
    <StavySkladu xmlns="">
      <Zbozi diffgr:id="Zbozi1" msdata:rowOrder="0" kod="C5709A" pocet="0"/>
      <Zbozi diffgr:id="Zbozi2" msdata:rowOrder="1" kod="C5708A" pocet="0"/>
      <Zbozi diffgr:id="Zbozi3" msdata:rowOrder="2" kod="C5718A" pocet="45"/>
      <Zbozi diffgr:id="Zbozi4" msdata:rowOrder="3" kod="EC13S050088" pocet="2"/>
      <Zbozi diffgr:id="Zbozi5" msdata:rowOrder="4" kod="EC376123GB" pocet="3"/>
      <Zbozi diffgr:id="Zbozi6" msdata:rowOrder="5" kod="EC800302" pocet="0"/>
      <Zbozi diffgr:id="Zbozi7" msdata:rowOrder="6" kod="EC806392" pocet="2"/>
      <Zbozi diffgr:id="Zbozi8" msdata:rowOrder="7" kod="EC800202" pocet="1"/>
      <Zbozi diffgr:id="Zbozi9" msdata:rowOrder="8" kod="EC806872" pocet="1"/>
      <Zbozi diffgr:id="Zbozi10" msdata:rowOrder="9" kod="EPC163071" pocet="16"/>
      <Zbozi diffgr:id="Zbozi11" msdata:rowOrder="10" kod="EMD157021" pocet="0"/>
      <Zbozi diffgr:id="Zbozi12" msdata:rowOrder="11" kod="EPC834031" pocet="0"/>
      <Zbozi diffgr:id="Zbozi13" msdata:rowOrder="12" kod="EPC825341" pocet="17"/>
    </StavySkladu>
  </diffgr:diffgram>
</DataSet>

1 个答案:

答案 0 :(得分:2)

<?php
$xml = new SimpleXMLElement($xmlstr);
$xml->registerXPathNamespace('xs', 'http://www.w3.org/2001/XMLSchema');
$path =' xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:attribute';

foreach($xml->xpath($path) as $child)
{
    $attributes = $child->attributes();
    echo "{$attributes['name']}\n";
}
?>