PHP/XML-如何读取多表子文件';s

PHP/XML-如何读取多表子文件';s,xml,simplexml,php,Xml,Simplexml,Php,我需要创建一个包含此XML文件中所有主题值的数组。 ISIN列表似乎工作正常(第一个属性值),但主题值不起作用 我希望最终得到一个类似以下内容的数组: $Companys = array ( [0] => array ( "isin" => "DK0010247014","company" => "AAB"), [1] => array ( "isin" => "DK0015250344","company" =>

我需要创建一个包含此XML文件中所有主题值的数组。 ISIN列表似乎工作正常(第一个属性值),但主题值不起作用

我希望最终得到一个类似以下内容的数组:

$Companys = array ( [0]  => array ( "isin" => "DK0010247014","company" => "AAB"),
                    [1]  => array ( "isin" => "DK0015250344","company" => "ALM BRAND"),
                    [2]  => array ( "isin" => "DK0015998017","company" => "BAVARIAN NORDI"),
                    [3]  => array ( "isin" => "DK0010259027","company" => "DFDS"),
                    [4]  => array ( "isin" => "DK0010234467","company" => "FLSMIDTH & CO"),
                );
这是我试图解析的其中一个文件的示例:

<doc>
    <id>123456</id>
    <version>4.0</version>
    <consnr>7861</consnr>
    <doctype>10</doctype>
    <dest>99</dest>
    <created>2013-05-15 14:18:16</created>
    <source>Direkt-DK</source>
    <language>DA</language>
    <texttype>This is a type</texttype>
    <premium>False</premium>
    <header>This is a header</header>
    <text>
        <para format="Text">This is a paragraph</para>
        <para format="Text">This is a paragraph</para>
        <para format="Text">This is a paragraph</para>
        <para format="Text">This is a paragraph</para>
        <para format="Text"/>
        <para format="Text">This is a paragraph</para>
        <para format="Byline"/>
        <para format="Byline">contents og the by line</para>
        <para format="Byline"/>
        <para format="Byline"/>
    </text>
    <subjects>
        <subject value="AAB" weight="Main">
            <property value="DK0010247014" type2="isin" type1="identificator"/>
            <property value="CSE:AAB" type2="ticker" type1="identificator"/>
            <property type1="sector" type2="GICS" type3="1" value="25"/>
            <property type1="sector" type2="GICS" type3="2" value="2530"/>
            <property type1="sector" type2="GICS" type3="3" value="253010"/>
            <property type1="sector" type2="GICS" type3="4" value="25301030"/>
        </subject>
        <subject value="ALM BRAND" weight="Main">
            <property value="DK0015250344" type2="isin" type1="identificator"/>
            <property value="CSE:ALMB" type2="ticker" type1="identificator"/>
            <property type1="sector" type2="GICS" type3="1" value="40"/>
            <property type1="sector" type2="GICS" type3="2" value="4030"/>
            <property type1="sector" type2="GICS" type3="3" value="403010"/>
            <property type1="sector" type2="GICS" type3="4" value="40301040"/>
        </subject>
        <subject value="BAVARIAN NORDI" weight="Main">
            <property value="DK0015998017" type2="isin" type1="identificator"/>
            <property value="CSE:BAVA" type2="ticker" type1="identificator"/>
            <property type1="sector" type2="GICS" type3="1" value="35"/>
            <property type1="sector" type2="GICS" type3="2" value="3520"/>
            <property type1="sector" type2="GICS" type3="3" value="352010"/>
            <property type1="sector" type2="GICS" type3="4" value="35201010"/>
        </subject>
        <subject value="DFDS" weight="Main">
            <property value="DK0010259027" type2="isin" type1="identificator"/>
            <property value="CSE:DFDS" type2="ticker" type1="identificator"/>
            <property type1="sector" type2="GICS" type3="1" value="20"/>
            <property type1="sector" type2="GICS" type3="2" value="2030"/>
            <property type1="sector" type2="GICS" type3="3" value="203030"/>
            <property type1="sector" type2="GICS" type3="4" value="20303010"/>
        </subject>
        <subject value="FLSMIDTH & CO" weight="Main">
            <property value="DK0010234467" type2="isin" type1="identificator"/>
            <property value="CSE:FLS" type2="ticker" type1="identificator"/>
            <property type1="sector" type2="GICS" type3="1" value="20"/>
            <property type1="sector" type2="GICS" type3="2" value="2010"/>
            <property type1="sector" type2="GICS" type3="3" value="201030"/>
            <property type1="sector" type2="GICS" type3="4" value="20103010"/>
        </subject>
    </subjects>
</doc>

123456
4
7861
10
99
2013-05-15 14:18:16
直达DK
DA
这是一种
假的
这是一个标题
这是一段
这是一段
这是一段
这是一段
这是一段
内容按行排列
脚本:

<?
    foreach($xmlObj->subjects->subject as $b ){
        $isin = $b->property;
        $company = $b->attributes();
        #$company = $b->attributes()->value;
        If($isin && $isinlist == 'null') $isinlist = $isin['value'];
        ElseIf ($isin && $isinlist) $isinlist .= ','.$isin['value'];
        If($company && $companylist == 'null') $companylist = $company['value'];
        ElseIf ($company && $companylist) $companylist .= ','.$company['value'];
        var_dump($company->value[0]);
    }
?>

您遇到的主要问题是根据属性值查找子元素。由于有多个子元素具有相同的元素名称,因此不能仅在名称上有所不同

在您的具体示例中,属性子级基于属性type2=“isin”

这可以通过使用Xpath(例如,本网站已经有很多关于Xpath的问答材料)或通过扩展
SimpleXMLElement
函数来实现:

class MyElement extends SimpleXMLElement
{
    public function getChildByAttributeValue($name, $value) {
        foreach($this as $child)
        {
            if ($value === (string) $child[$name]) {
                return $child;
            }
        }
    }
}
然后可以使用
MyElement
而不是
simplexmlement

$xml = simplexml_load_string($buffer, 'MyElement');
                                      ###########
只需将值映射到数组:

$map = function(MyElement $subject) {
    return [
        (string) $subject['value'],
        (string) $subject->getChildByAttributeValue('type2', 'isin')['value'],
    ];
};

print_r(array_map($map, $xml->xpath('//subject')));
鉴于
$buffer
是您提供的有问题的XML(并且编码错误已删除),这将创建以下输出:

Array
(
    [0] => Array
        (
            [0] => AAB
            [1] => DK0010247014
        )

    [1] => Array
        (
            [0] => ALM BRAND
            [1] => DK0015250344
        )

    [2] => Array
        (
            [0] => BAVARIAN NORDI
            [1] => DK0015998017
        )

    [3] => Array
        (
            [0] => DFDS
            [1] => DK0010259027
        )

    [4] => Array
        (
            [0] => FLSMIDTH & CO
            [1] => DK0010234467
        )

)
完整的代码示例():


请将您的问题降至演示问题所需的最低限度。要触发该错误,您不需要所有的XML或PHP代码。在网站上创建问题时,请保持简洁。这样,您可能已经找到了原因或解决方案(但您仍然可以询问您不理解的内容(提示)),并且您将得到更好的答案。我需要将每个主题中的值(如“FLSMIDTH&CO”)解析为一个值字符串,逗号分隔。我将编辑我的问题,使其更具体。
class MyElement extends SimpleXMLElement
{
    public function getChildByAttributeValue($name, $value) {
        foreach($this as $child)
        {
            if ($value === (string) $child[$name]) {
                return $child;
            }
        }
    }
}

$xml = simplexml_load_string($buffer, 'MyElement');

$map = function(MyElement $subject) {
    return [
        (string) $subject['value'],
        (string) $subject->getChildByAttributeValue('type2', 'isin')['value'],
    ];
};

print_r(array_map($map, $xml->xpath('//subject')));