php和xml,这是读取所有节点的更好方法


php and xml, a better way to read all nodes

我有这个xml,我找不到用PHP读取所有这些数据的方法。我尝试过简单的:

$xmlDoc->load("data/xml/page.xml");

对于所有其他内容,但在某种程度上,我找不到读取图库名称(在这个xml中)和图像的url/属性的方法

<DOCUMENT>  <galleries_group>
<GROUP id="main_gallery_group">
  <DATA id="category_name">
    <CONTENT><![CDATA[Black & White]]></CONTENT>
  </DATA>
  <GROUP id="sub_gallery_group">
    <DATA id="gallery_name">
      <CONTENT><![CDATA[WOMENS]]></CONTENT>
    </DATA>
    <GROUP id="sub_gallery_image">
      <DATA id="gallery_thumb">
        <CONTENT>
          <SRC width="277" height="366">images/black_white/005_th.jpg</SRC>
        </CONTENT>
      </DATA>
      <DATA id="gallery_image">
        <CONTENT>
          <SRC width="817" height="1080">images/black_white/005.jpg</SRC>
        </CONTENT> </DATA>
    </GROUP>
    <GROUP id="sub_gallery_image">
      <DATA id="gallery_thumb">
        <CONTENT>
          <SRC width="515" height="366">images/black_white/006_th.jpg</SRC>
        </CONTENT>
      </DATA>
      <DATA id="gallery_image">
        <CONTENT>
          <SRC width="1521" height="1080">images/black_white/006.jpg</SRC>
        </CONTENT>
      </DATA>
    </GROUP>
  </GROUP><GROUP id="main_gallery_group">
  <DATA id="category_name">
    <CONTENT><![CDATA[Gallery ..n]]></CONTENT>
  </DATA>
  <GROUP id="sub_gallery_group">
    <DATA id="gallery_name">
      <CONTENT><![CDATA[GROUP 1]]></CONTENT>
    </DATA>
    <GROUP id="sub_gallery_image">
      <DATA id="gallery_thumb">
        <CONTENT>
          <SRC width="484" height="366">images/gallery/01_th.jpg</SRC>
        </CONTENT>
      </DATA>
      <DATA id="gallery_image">
        <CONTENT>
          <SRC width="1429" height="1080">images/gallery/01.jpg</SRC>
        </CONTENT>
      </DATA>
    </GROUP> 
    <GROUP id="sub_gallery_image">
      <DATA id="gallery_thumb">
        <CONTENT>
          <SRC width="399" height="366">images/gallery/02_th.jpg</SRC>
        </CONTENT>
      </DATA>
      <DATA id="gallery_image">
        <CONTENT>
          <SRC width="981" height="900">images/gallery/02.jpg</SRC>
        </CONTENT>
      </DATA>
    </GROUP>
    </GROUP>
  <GROUP id="sub_gallery_group">
    <DATA id="gallery_name">
      <CONTENT><![CDATA[GROUP N]]></CONTENT>
    </DATA><GROUP id="sub_gallery_image">
      <DATA id="gallery_thumb">
        <CONTENT>
          <SRC width="483" height="366">images/gallery/09_th.jpg</SRC>
        </CONTENT>
      </DATA>
      <DATA id="gallery_image">
        <CONTENT>
          <SRC width="1065" height="600">images/gallery/09.jpg</SRC>
        </CONTENT>
      </DATA>
    </GROUP>
    <GROUP id="sub_gallery_image">
      <DATA id="gallery_thumb">
        <CONTENT>
          <SRC width="235" height="366">images/gallery/10_th.jpg</SRC>
        </CONTENT>
      </DATA>
      <DATA id="gallery_image">
        <CONTENT>
          <SRC width="1920" height="1080">images/gallery/10.jpg</SRC>
        </CONTENT>
      </DATA>
    </GROUP>
   </GROUP> 
  </GROUP>     
</GROUP>   <GROUP id="text_pages_group">
<GROUP id="text_page_group">
  <DATA id="text_page_name">
    <CONTENT><![CDATA[About]]></CONTENT>
  </DATA>
  <DATA id="text_page_text">
    <CONTENT><![CDATA[text12]]></CONTENT>
  </DATA>
</GROUP></GROUP><GROUP id="text_pages_group"><GROUP id="text_page_group">
  <DATA id="text_page_name">
    <CONTENT><![CDATA[Contact]]></CONTENT>
  </DATA>
  <DATA id="text_page_text">
    <CONTENT><![CDATA[text22]]></CONTENT>
  </DATA>
</GROUP></GROUP></DOCUMENT>

当然,这是一个小xml(只是举个例子),但完整的xml具有相同的格式类型,有什么帮助吗?

您可能想要尝试类似的操作,并根据需要调整XPath。

<?php
$xmlDoc = simplexml_load_file('data/xml/page.xml');
$result = $xmlDoc->xpath('/DOCUMENT//GROUP/DATA/CONTENT');
while (list(, $node) = each($result)) {
    echo $node;
}
?>

不过要注意,因为您最初发布的XML格式不正确。线路1中的CCD_ 1要么没有正确闭合,要么完全不可分配。