Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用此PHP脚本仅显示XML中的第二项?_Php_Xml - Fatal编程技术网

如何使用此PHP脚本仅显示XML中的第二项?

如何使用此PHP脚本仅显示XML中的第二项?,php,xml,Php,Xml,如何使用此PHP脚本仅显示XML中的第二项 <xml> <item> <name>Examplename</name> <amount>0000</amount> </item> <item> <name>Examplename</name> <amount>0000</a

如何使用此PHP脚本仅显示XML中的第二项

<xml>
    <item>
        <name>Examplename</name>
        <amount>0000</amount>
    </item>
    <item>
        <name>Examplename</name>
        <amount>0000</amount>
    </item>
    <item>
        <name>Examplename</name>
        <amount >0000</amount>
    </item>
    <item>
        <name>DExamplename</name>
        <amount>0000</amount>
    </item>
</xml>


<?

// DOMElement->getElementsByTagName() -- Gets elements by tagname
// nodeValue : The value of this node, depending on its type.
// Load XML File. You can use loadXML if you wish to load XML data from a string

$objDOM = new DOMDocument();
$objDOM->load("feed.xml"); //make sure path is correct


$note = $objDOM->getElementsByTagName("item");

// for each note tag, parse the document and get values for
// tasks and details tag.

foreach( $note as $value ){


$places = $value->getElementsByTagName("name");
$place  = $places->item(0)->nodeValue;

$details = $value->getElementsByTagName("amount");
$detail  = $details->item(0)->nodeValue;




echo "<BODY STYLE='background-color:transparent'>

<table border='0'>
  <tr>
    <td width='127'><table width='127px' border='0' cellpadding='0' cellspacing='0'>
<tr>
<td><p style='font-family:arial;color:white;font-size:22px;padding-left:3px;text-align:left;  background:url(bg2.png)'><strong> $place</strong></p></td>

</tr>

</table>
</td>
    <td width='115'>
      <table width='115px' border='0' cellpadding='0' cellspacing='0'>
        <tr>
          <td><p style='font-family:arial;color:white;font-size:22px;text-align:right;padding-right:5px; background:url(bg1.png)'><strong>$detail</strong></p></td>

        </tr>

</table></td>
  </tr>

</table>




";
 $i++; }
?> 

示例名称
0000
示例名称
0000
示例名称
0000
地塞米松
0000
加载(“feed.xml”)//确保路径正确
$note=$objDOM->getElementsByTagName(“项目”);
//对于每个note标记,解析文档并获取其值
//任务和详细信息标签。
foreach(按$value计价的票据){
$places=$value->getElementsByTagName(“名称”);
$place=$places->item(0)->nodeValue;
$details=$value->getElementsByTagName(“金额”);
$detail=$details->item(0)->nodeValue;
回声“

$detail

"; $i++;} ?>
您需要利用

使用:


文本
文本2
STR;
$xml=simplexml\u load\u字符串($str);
$second_item=$xml->xpath('//item[2]');//注意[2]!
var_出口(第二项);
使用和:


示例名称
0000
示例名称
0000
示例名称
0000
地塞米松
0000
STR;
$xml=DOMDocument::loadXML($str);
$xpath=newdomxpath($xml);
$x=$xpath->query('//item[2]');
var_导出($x->item(0)->nodeValue);

但我无法修改XML文件,因为它是由其他人托管的,许多其他网站都使用该提要。还有其他建议吗?顺便说一句,谢谢你的帮助。你不需要修改XML。您只需使用SimpleXML或DOCDocument加载它。我得到了需要显示的行,一切正常,谢谢。但是我如何使它不必自己输入XML数据,而是从原始提要插入?我走了SimpleXML的路。
<?php
$str = <<<STR
<?xml version="1.0" encoding="utf-8"?>
<XmlDoc>
    <item>
        text
    </item>
    <item>
        text2
    </item>
</XmlDoc>
STR;
$xml = simplexml_load_string($str);
$second_item = $xml->xpath('//item[2]'); // notice [2]!
var_export($second_item);
<?php
$str = <<<STR
<?xml version="1.0" encoding="utf-8"?>
<xml>
    <item>
        <name>Examplename</name>
        <amount>0000</amount>
    </item>
    <item>
        <name>Examplename</name>
        <amount>0000</amount>
    </item>
    <item>
        <name>Examplename</name>
        <amount >0000</amount>
    </item>
    <item>
        <name>DExamplename</name>
        <amount>0000</amount>
    </item>
</xml>
STR;

$xml = DOMDocument::loadXML($str);

$xpath = new DOMXpath($xml);
$x = $xpath->query('//item[2]');
var_export($x->item(0)->nodeValue);