使用PHP解析XML格式的选项和optgroup

时间:2015-05-08 08:42:45

标签: php xml

我有一个定义表单字段的xml文件。对于带有optgroups的列表,我的xml将包含类似

的内容
<field
    name="expertise"
    type="multilist"
    label="Area of Expertise"
>
    <option value="Disaster Management">Disaster Management </option>
    <option value="Energy Security">Energy Security </option>
    <optgroup label="Environment">
        <option value="Climate">Climate </option>
        <option value="Resource Security">Resource Security </option>
    </optgroup>
</field>

我试图循环遍历

等节点
foreach ($feed->fieldset[1]->field[$j]->children() as $c):
    foreach($c->attributes() as $key => $value):
        echo '<'.$c->getName().' '.$key.'="'.$value.'">'.$c.'</'.$c->getName().'><br>';
    endforeach;
endforeach;

我知道,对于等于标签的价值可能没有意义,但我稍后会担心。

目前,我正试图弄清楚如何遍历optgroup中的选项和选项。上面的循环没有显示optgroup的选项(如预期的那样)。似乎我可能不得不做一些递归,但是想知道是否有更好的方法可以推荐,比如现有的php xml函数可以处理我想要做的事情(到目前为止我还没有找到任何东西)。

谢谢!

2 个答案:

答案 0 :(得分:0)

use XML parser

<?xml version="1.0" encoding="utf-8"?>
<dir name="h:\">
  <dir name="System Volume Information" />
  <dir name="xp1000" />
  <dir name="Procura_Desenhos">
    <dir name="Procura_Desenhos">
      <dir name="Resources" />
      <dir name="Properties" />
      <dir name="obj">
        <dir name="Release" />
        <dir name="Debug">
          <dir name="TempPE" />
          <dir name="Refactor" />
        </dir>
      </dir>
      <dir name="bin">
        <dir name="Release" />
        <dir name="Debug" />
      </dir>
    </dir>
  </dir>
  <dir name="Procura_Desenhos1.1">
    <dir name="Procura_Desenhos1.1">
      <dir name="bin">
        <dir name="Debug" />
        <dir name="Release" />
      </dir>
      <dir name="obj">
        <dir name="Debug">
          <dir name="TempPE" />
        </dir>
      </dir>
      <dir name="Properties" />
      <dir name="Resources" />
    </dir>
  </dir>
  <dir name="Backup">
    <dir name="Visual 2008">
      <dir name="Projects">
        <dir name="Procura_Desenhos">
          <dir name="Procura_Desenhos">
            <dir name="Resources" />
            <dir name="Properties" />
            <dir name="obj">
              <dir name="Release" />
              <dir name="Debug">
                <dir name="TempPE" />
                <dir name="Refactor" />
              </dir>
            </dir>
            <dir name="bin">
              <dir name="Release" />
              <dir name="Debug" />
            </dir>
          </dir>
        </dir>
      </dir>
    </dir>
    <dir name="Visual 2005">
      <dir name="Projects">
        <dir name="WindowsApplication1">
          <dir name="WindowsApplication1">
            <dir name="obj">
              <dir name="Release" />
              <dir name="Debug">
                <dir name="TempPE" />
              </dir>
            </dir>
            <dir name="My Project" />
            <dir name="bin">
              <dir name="Release" />
              <dir name="Debug" />
            </dir>
          </dir>
        </dir>
        <dir name="Procura">
          <dir name="Procura">
            <dir name="Resources" />
            <dir name="obj">
              <dir name="Release" />
              <dir name="Debug">
                <dir name="TempPE" />
              </dir>
            </dir>
            <dir name="My Project" />
            <dir name="bin">
              <dir name="Release" />
              <dir name="Debug" />
            </dir>
          </dir>
        </dir>
        <dir name="find_v1.1">
          <dir name="_UpgradeReport_Files" />
          <dir name="find_v1.1">
            <dir name="Properties" />
            <dir name="obj">
              <dir name="Release" />
              <dir name="Debug">
                <dir name="TempPE" />
              </dir>
            </dir>
            <dir name="bin">
              <dir name="Release" />
              <dir name="Debug" />
            </dir>
          </dir>
          <dir name="Backup">
            <dir name="find_v1.1">
              <dir name="Properties" />
            </dir>
          </dir>
        </dir>
        <dir name="find_v1.0">
          <dir name="find_v1.0">
            <dir name="Properties" />
            <dir name="obj">
              <dir name="Release" />
              <dir name="Debug">
                <dir name="TempPE" />
              </dir>
            </dir>
            <dir name="bin">
              <dir name="Release" />
              <dir name="Debug" />
            </dir>
          </dir>
        </dir>
      </dir>
    </dir>
  </dir>
</dir>

http://php.net/manual/en/book.xml.php

答案 1 :(得分:0)

我最终让它像这样工作。主要的是我需要能够标记为之前选择的选定选项。

If pptShape.Type = msoLinkedOLEObject Or 
   pptShape.Type = msoLinkedPicture Or pptShape.Type = msoChart Then