将xml文件输入到元素中的多个元素?

时间:2013-12-09 08:58:57

标签: php xml input

你好我输入以下XML因为多个“作者”而遇到很多麻烦..当我尝试使用1但是多次失败时,它可以正常工作。有谁知道我怎么能做多个作者输入?下面是我的xml和代码的例子

<pub>
    <ID>3</ID>
    <title>A Sampling Based Approach to Facial Feature Extraction</title>
    <year>2005</year>
    <booktitle>AutoID</booktitle>
    <pages>51-56</pages>
    <authors>
        <author>john johnson</author>
        <author>billy bob</author>
    </authors>
</pub>

我的代码:

<?php

$pubs = new SimpleXMLElement('p.xml', null, true);

echo <<<EOF
<table>
    <tr>
            <th>ID</th>
            <th>title</th>
            <th>year</th>
            <th>booktitle</th>
            <th>pages</th>
            <th>authors</th>
    </tr>

EOF;
foreach($pubs as $pub)
{
    echo <<<EOF
    <tr>
            <td>{$pub->ID}</td>
            <td>{$pub->title}</td>
            <td>{$pub->year}</td>
            <td>{$pub->booktitle}</td>
            <td>{$pub->pages}</td>
            <td>{$pub->authors}</td>
    </tr>


EOF;
}

echo '</table>';
?>

2 个答案:

答案 0 :(得分:1)

我认为,最简单的方法是将其转换为json字符串而不是返回数组,您将看到它是一个数组,因此您需要另一个 foreach     

$pubs = new SimpleXMLElement('p.xml', null, true);    
$jsonString = json_encode($pubs);
$assocArray = json_decode($jsonString, true);

echo '<pre>';
var_dump($assocArray);
echo '</pre>';

对不起。我给你错误的信息,因为如果你尝试了这个代码,如果你有超过1个''标签并且你只有1个,你会得到不同的结果。

所以我有另一个解决方案:

<?php
$pubs = new SimpleXMLElement('test.xml', null, true);

echo <<<EOF
<table>
    <tr>
            <th>ID</th>
            <th>title</th>
            <th>year</th>
            <th>booktitle</th>
            <th>pages</th>
            <th>authors</th>
    </tr>

EOF;

$pubsXpath = $pubs->xpath('//pub');
/** @var SimpleXMLElement $pub */
foreach ($pubsXpath as $pub) {
    $authorsSxml = $pub->xpath('authors/author');
    $authors = join(', ', $authorsSxml);
    echo <<<EOF
    <tr>
            <td>{$pub->ID}</td>
            <td>{$pub->title}</td>
            <td>{$pub->year}</td>
            <td>{$pub->booktitle}</td>
            <td>{$pub->pages}</td>
            <td>{$authors}</td>
    </tr>
EOF;
}
echo '</table>';
?>

答案 1 :(得分:1)

作者是几个节点,所以你必须使用第二个循环。此外,如果以HTML / XML输出值,则需要转义它们。 htmlspecialchars()为此做了足够好的工作。

您正在将XML转换为HTML。 XSLT将是一个更好的工具。它受PHP中的XSL扩展支持。

在PHP中,您可以使用Xpath从XML中提取数据:

$dom = new DOMDocument();
$dom->load('p.xml');
$xpath = new DOMXpath($dom);

echo <<<EOF
<table>
  <tr>
    <th>ID</th>
    <th>title</th>
    <th>year</th>
    <th>booktitle</th>
    <th>pages</th>
    <th>authors</th>
  </tr>
EOF;

$pubs = $xpath->evaluate('//pub');
foreach($pubs as $pub) {
  echo '<tr>', "\n";
  echo '<td>', htmlspecialchars($xpath->evaluate('string(./ID)', $pub)), '</td>', "\n";
  echo '<td>', htmlspecialchars($xpath->evaluate('string(./title)', $pub)), '</td>', "\n";
  echo '<td>', htmlspecialchars($xpath->evaluate('string(./year)', $pub)), '</td>', "\n";
  echo '<td>', htmlspecialchars($xpath->evaluate('string(./booktitle)', $pub)), '</td>', "\n";
  echo '<td>', htmlspecialchars($xpath->evaluate('string(./pages)', $pub)), '</td>', "\n";

  // fetch all the authors into an array
  $authors = array();
  foreach ($xpath->evaluate('./authors/author', $pub) as $author) {
    $authors[] = $author->nodeValue;
  }
  echo '<td>'.htmlspecialchars(implode(', ', $authors)).'</td>', "\n";
  echo '</tr>', "\n";
}
echo '</table>';

该示例使用DOMXpath :: evaluate(),因为它允许直接从DOM查询字符串。 $xpath->evaluate('string(./ID)', $pub)返回上下文节点$ pub的ID子元素内的文本内容,如果此处没有子元素ID,则返回空字符串。

相关问题