从php读取xml输出并将其存储在变量中

时间:2015-05-31 16:19:47

标签: php xml

我希望使用php存储domorder下的所有值,就像下面的数组变量一样,在xml文件中有时$ tld [veribal] [$ i]可能有多个值 注意:xml文件来自url

$tld[0] = dombiz;
$tld[dombiz][0] = biz;
$tld[1] = dominfo;
$tld[dominfo][0] = info;

xml文件:

<hashtable>
    <entry>
    <string>domorder</string>
    <map-array>
      <map>
        <entry>
          <string>dombiz</string>
          <vector>
            <string>biz</string>
          </vector>
        </entry>
      </map>
      <map>
        <entry>
          <string>dominfo</string>
          <vector>
            <string>info</string>
          </vector>
        </entry>
      </map>
      <map>
        <entry>
          <string>domcno</string>
          <vector>
            <string>com</string>
          </vector>
        </entry>
      </map>
      <map>
        <entry>
          <string>domorg</string>
          <vector>
            <string>org</string>
          </vector>
        </entry>
      </map>
      <map>
        <entry>
          <string>domus</string>
          <vector>
            <string>us</string>
          </vector>
        </entry>
      </map>
      <map>
        <entry>
          <string>donutsgroup2</string>
          <vector>
            <string>fish</string>
            <string>chat</string>
            <string>associates</string>
            <string>media</string>
          </vector>
        </entry>
      </map>
      <map>
        <entry>
          <string>dotlove</string>
          <vector>
            <string>love</string>
          </vector>
        </entry>
      </map>
    </map-array>
  </entry>
  <entry>
    <string>digicert</string>
    <string-array/>
  </entry>
</hashtable>

1 个答案:

答案 0 :(得分:0)

此代码仅打印dombizbiz,但您可以进行迭代。

    $myXMLData = "<hashtable>
    <entry>
    <string>domorder</string>
    <map-array>
      <map>
        <entry>
          <string>dombiz</string>
          <vector>
            <string>biz</string>
          </vector>
        </entry>
      </map>
      <map>
        <entry>
          <string>dominfo</string>
          <vector>
            <string>info</string>
          </vector>
        </entry>
      </map>
      <map>
        <entry>
          <string>domcno</string>
          <vector>
            <string>com</string>
          </vector>
        </entry>
      </map>
      <map>
        <entry>
          <string>domorg</string>
          <vector>
            <string>org</string>
          </vector>
        </entry>
      </map>
      <map>
        <entry>
          <string>domus</string>
          <vector>
            <string>us</string>
          </vector>
        </entry>
      </map>
      <map>
        <entry>
          <string>donutsgroup2</string>
          <vector>
            <string>fish</string>
            <string>chat</string>
            <string>associates</string>
            <string>media</string>
          </vector>
        </entry>
      </map>
      <map>
        <entry>
          <string>dotlove</string>
          <vector>
            <string>love</string>
          </vector>
        </entry>
      </map>
    </map-array>
  </entry>
  <entry>
    <string>digicert</string>
    <string-array/>
  </entry>
</hashtable>";

$xml = simplexml_load_string($myXMLData);
$json = json_encode($xml);
$tld = json_decode($json,TRUE);

print($tld['entry'][0]['map-array']['map'][0]['entry']['string']); // dombiz
print('<br />');
print($tld['entry'][0]['map-array']['map'][0]['entry']['vector']['string']); // biz