-1

如何将标签引用分配给文本?

$xml =<<<EOT
<?xml version="1.0"?>
<root>
<section name="Section2">
  <category id="category1_of_section2" name="msn">
  teste
   <arti name="article2">value1</arti>
   <arti name="article3">value2 <sref ref="article6" />value6 <sref ref="article7" />value7</arti>
   <arti name="article4">value3 <sref ref="article8" />value8</arti>
  </category>
  <category id="Category2_of_section2" name="webcare">
    <arti name="param3">value4</arti>
  </category>
</section>
</root>
EOT;

$dom = new DomDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->loadXML($xml);

$params = $dom->getElementsByTagName('section'); // Find Sections 
$k=0;
$numero2=0;
$roomStr3="";
foreach ($params as $param)
{
         echo "Section Attribute :-> ".$params->item($k)->getAttribute('name')."<br>";
         $params2 = $params->item($k)->getElementsByTagName('category');
      $i=0;
        foreach ($params2 as $p) 
        {
            echo "&nbsp;&nbsp;- Category Attribute Name :-> ".$params2->item($i)->getAttribute('name')."<br>";
            $params3 = $params2->item($i)->getElementsByTagName('arti');
                $j=0;

                    foreach ($params3 as $p2)
                    {

                    echo "&nbsp;&nbsp;&nbsp;- Article Attribute Name : ".$params3->item($j)->getAttribute('name')."";
                    echo "&nbsp;&nbsp; Value : ".$params3->item($j)->nodeValue."<br>";

                    var_dump($params3->item($j));
                    $elems3 = $params3->item($j)->getElementsByTagName('sref');

                    $j++;
                    }
        $i++;
      }
$k++;
}
//Print
    Section Attribute :-> Section2
      - Category Attribute Name :-> msn
       - Article Attribute Name : article2   Value : value1
       - Article Attribute Name : article3   Value : value2 value6 value7
       - Article Attribute Name : article4   Value : value3 value8
      - Category Attribute Name :-> webcare
       - Article Attribute Name : param3   Value : value

如何显示参考?

前任:

  • 文章属性名称:article3 值:value2 ref="article6": value6 ref="article7": value7

  • 文章属性名称:article4 值:value3 ref="article8": value8

4

1 回答 1

0

好吧,你需要这个getAttribute函数来做到这一点。

您已经使用过它:

echo "&nbsp;&nbsp;&nbsp;- Article Attribute Name : ".$params3->item($j)->getAttribute('name')."";

只需对<sref>-element 执行相同操作:

$elems3 = $params3->item($j)->getElementsByTagName('sref');
$elems3Attribute = $elems3->getAttribute('ref');
于 2012-11-05T21:48:57.227 回答