0

我想显示通过字段名称和值区分文档的 xml 内容,请提供任何帮助。以下是我的 xml 文件内容。

<doc>
    <field name="id">1</field>
    <field name="name">A</field>
    <field name="sk">Acce</field>
    <field name="level">Beginner</field>
    <field name="do">Tuto</field>
    <field name="open">1</field>
    <field name="type">Ct</field>
    <field name="extensis">cl_ex</field>
    <field name="features">Atos</field>
    <field name="downl"></field>
    <field name="source">AtomicCounters</field>
    <field name="description">Ths.</field>
    <field name="tag1"></field>
    <field name="tag2"></field>
    <field name="tag3"></field>
    <field name="tag4">Global</field>
    <field name="tag5">Atomic</field>
    <field name="tag6"></field>
    <field name="tag7"></field>
    <field name="tag8"></field>
    <field name="tag9"></field>
    <field name="tag10"></field>
  </doc>

  <doc>
    <field name="id">2</field>
    <field name="name">Ar</field>
    <field name="sk">Acrce</field>
    <field name="level">Beginner</field>
    <field name="do">Tuto1</field>
    <field name="open">11</field>
    <field name="type">C1t</field>
    <field name="extensis">cl_exd</field>
    <field name="features">Atos</field>
    <field name="downl"></field>
    <field name="source">ddddd</field>
    <field name="description">Thsdd.</field>
    <field name="tag1"></field>
    <field name="tag2"></field>
    <field name="tag3"></field>
    <field name="tag4">Global1</field>
    <field name="tag5">Atomic1</field>
    <field name="tag6"></field>
    <field name="tag7"></field>
    <field name="tag8"></field>
    <field name="tag9"></field>
    <field name="tag10"></field>
  </doc>

下面是我正在使用的代码..

 <?php
    $result="";
    $document = new DOMDocument();
    //$document->load('/wamp/www/amd/261013/test/amd_one.xml');
    $document->load('http://localhost/solr/select');



    // this will also output doctype and comments at top level
    foreach($document->childNodes as $node)
       $result[] .= $document->saveXML($node)."\n";

    //echo $result[];

    foreach($result as $key => $value)
    {
      echo "$key - $value<br/>";
    }

    ?>

然而,它只是价值......就像1 Ar Acce......

我想要像 $field = id, $value = 1,$field = name, $value = A, $field = id, $value = 2,$field = name, $value = Ar,..

请有任何建议..

4

2 回答 2

0

这不是关于keyand value,而是关于 -node 的name-attribute<field>及其值。

使用示例simplexml

$xml = simplexml_load_string($x); // assume XML in $x

foreach ($xml->doc as $doc) {
    foreach ($doc->field as $field) echo "$field[name]: $field <br />";
    echo "------ <br />";
}

看到它工作:http ://codepad.viper-7.com/k3zg8I

于 2013-10-26T15:09:26.570 回答
0

您的代码中有一些问题。首先,你真的不需要转换成一个(你可以,但要让事情开始,只需做尽可能少的事情)。还有一些我猜你不知道的点,所以这里有一些关于你的代码的指针:

你写:

foreach ($document->childNodes as $node)

但实际上你目前只对<doc>元素感兴趣。我们开工吧:

foreach ($document->getElementsByTagName('doc') as $node)

这个变体实际上只会给你<doc>你感兴趣的元素$node

然后你遇到的下一个问题是将<field>(在我的答案中称为<str>)元素及其name属性和节点值转换为一$key => $value对。我想说这最好用一点帮助映射功能来完成,这样你就可以更流畅地应用转换。让我们创建这样一个映射函数:

$keyValue = function (DOMElement $element)
{
    return [
        $element->getAttribute('name'), // key
        trim($element->nodeValue),      // value
    ];
};

这是一个小函数,它将一个DOMElement对象转换为一个数组,其中键作为第一个条目(通过读取属性name)和值作为第二个条目(通过读取节点值)。这是一个非常简单的功能。

现在可以在$docs元素的循环中应用此函数,并进一步向下应用所有<str>子元素:

foreach ($document->getElementsByTagName('doc') as $node)
{
    foreach ($node->getElementsByTagName('str') as $field)
    {
        list($key, $value) = $keyValue($field);
        echo "\$key: $key - \$value: $value\n";
    }
}

正如这个块显示的那样,在遍历<doc>元素之后,您遍历每个名​​为<str>(<field>在您的问题中) 的后代元素。在这些中的每一个上$keyValue()都应用了映射函数,您可以从映射函数的返回值中获取$keyand$value变量。

注意:如果您需要更具体的子节点遍历,请参阅PHP DOMDocument 中的过滤子节点遍历和类似的更多想法,以了解如何迭代DOMNodeList和朋友。

让我们运行示例来测试它是否有效:

1. Node:
========
$key: id - $value: 3
$key: name - $value: BinarySearch
$key: sdk - $value: Accelerated Parallel Process
$key: level - $value: Beginner
$key: domain - $value: 
...
2. Node:
========
$key: id - $value: 2
$key: name - $value: BasicDebug
$key: sdk - $value: Accelerated Parallel Process
$key: level - $value: Beginner
$key: domain - $value: Debug
...
3. Node:
========
$key: id - $value: 1
$key: name - $value: AtomicCounters
$key: sdk - $value: Accelerated Parallel Process
$key: level - $value: Beginner
$key: domain - $value: Tutorial
...
4. Node:
========
$key: id - $value: 4
$key: name - $value: BinomialOption
$key: sdk - $value: Accelerated Parallel Process
$key: level - $value: Beginner
$key: domain - $value: Financial Engineering
...
5. Node:
========
$key: id - $value: 5
$key: name - $value: BinomialOptionMultiGPU
$key: sdk - $value: Accelerated Parallel Process
$key: level - $value: Intermediate
$key: domain - $value: Financial Engineering
...
6. Node:
========
$key: id - $value: 6
$key: name - $value: BitonicSort
$key: sdk - $value: Accelerated Parallel Process
$key: level - $value: Beginner
$key: domain - $value: General
...
7. Node:
========
$key: id - $value: 7
$key: name - $value: BlackScholes
$key: sdk - $value: Accelerated Parallel Process
$key: level - $value: Intermediate
$key: domain - $value: Financial Engineering
...
8. Node:
========
$key: id - $value: 8
$key: name - $value: BlackScholesDP
$key: sdk - $value: Accelerated Parallel Process
$key: level - $value: Intermediate
$key: domain - $value: Financial Engineering
...
9. Node:
========
$key: id - $value: 9
$key: name - $value: BoxFilter
$key: sdk - $value: Accelerated Parallel Process
$key: level - $value: Intermediate
$key: domain - $value: Image Processing
...
10. Node:
========
$key: id - $value: 10
$key: name - $value: BoxFilterGL
$key: sdk - $value: Accelerated Parallel Process
$key: level - $value: Intermediate
$key: domain - $value: Image Processing
...

这看起来就像您正在寻找的结果。这是该示例中使用的确切代码:

<?php
/**
 * How to display value with field names from Solr XML files, getting values only
 *
 * @link https://stackoverflow.com/q/19605287/367456
 */

$file = "solr.xml";

if (!file_exists($file))
{
    $url    = 'http://index.websolr.com/solr/15b84f04a7e/select';
    $handle = fopen($url, 'r');
    file_put_contents($file, $handle);
    fclose($handle);
}

$document = new DOMDocument();
$document->load($file);

$keyValue = function (DOMElement $element)
{
    return [
        $element->getAttribute('name'), // key
        trim($element->nodeValue), // value
    ];
};


foreach ($document->getElementsByTagName('doc') as $node)
{
    foreach ($node->getElementsByTagName('str') as $field)
    {
        list($key, $value) = $keyValue($field);
        echo "\$key: $key - \$value: $value\n";
    }
}


foreach ($document->getElementsByTagName('doc') as $index => $node)
{
    printf("%d. Node:\n========\n", $index + 1);
    foreach ($node->getElementsByTagName('str') as $count => $field)
    {
        if ($count > 4)
        {
            echo "...\n";
            break;
        }

        list($key, $value) = $keyValue($field);
        echo "\$key: $key - \$value: $value\n";
    }
}
于 2013-10-26T19:17:17.703 回答