drudfe0446838 2018-10-22 16:11 采纳率: 0%
浏览 82

来自xml-file的PHP数组 - 获取未知的对象名称和属性

I load a xml-file like this:

$xml = simplexml_load_file('test.xml'); 

Then I created an array:

$array= $xml->xpath('//objektart');

The content of the array looks like this:

Array
(
    [0] => SimpleXMLElement Object
        (
            [haus] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [haustyp] => ZWEIFAMILIENHAUS
                        )

                )

        )

)

Now I want to get the name of the SimpleXMLElement Object = "haus" and the attributes = "haustyp" and "ZWEIFAMILIENHAUS" for outputtting them on a website.

The values like "haus", "haustyp" and "ZWEIFAMILIENHAUS" can vary, so i cant rely on these "keywords" to access them. Just the method of creating the array from the xml-element stays the same.

Can anyone please point me to the right direction here? I'm stuck for hours testing without any result...

  • 写回答

1条回答 默认 最新

  • donglang1976 2018-10-23 15:31
    关注

    I created a package to simplify xml conversions in php. You can find it here:

    https://github.com/mtownsend5512/xml-to-array/blob/master/src/XmlToArray.php

    It will allow you to convert your xml like so:

    $xml = \Mtownsend\XmlToArray\XmlToArray::convert(file_get_contents('test.xml'));
    

    Then you may inspect the php array with print_r($xml); If attributes are picked up, they will be in an @attributes key. You are free to use PHP's many array functions to manipulate and loop over the array.

    If you provide more information about your actual xml, I can provide more detailed examples.

    评论

报告相同问题?

悬赏问题

  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答