如何使用Solr XML文件中的字段名称显示值,仅获取值

时间:2013-10-26 09:57:01

标签: php xml solr

我想显示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,..

任何想法都请..

2 个答案:

答案 0 :(得分:0)

这不是关于keyvalue,而是关于name - <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

答案 1 :(得分: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(),并从映射函数返回值中获取$key$value变量。

  

注意:如果您需要更具体的子节点遍历,请参阅filtered childnode traversal in 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";
    }
}