【问题标题】:Getting attributes for XMLReader获取 XMLReader 的属性
【发布时间】:2013-07-11 01:06:03
【问题描述】:

我在提取以下 XML 的属性时遇到问题。我可以获得以下属性:

<Formats>
   <Format Type="6" Price="7.00" MaxDownloads="3" Status="active" ExclusiveRights="CA AU NZ ZA GB IE " NotForSale="AU NZ " />
 </Formats>

但我正在努力获取以下属性:

<Contributors>
    <Contributor Code="A01" Text="By (author)">Cairns, Warwick</Contributor>
   </Contributors>

我需要“A01”和“Cairns, Warwick”。

非常感谢任何帮助。

朱利安

下面的 XML:

<?xml version="1.0"?>
    <Extract>
      <EBook EAN="9781849892100">
        <Title>About The Size Of It</Title>
        <SubTitle>A Common Sense Approach To How People Measure Things</SubTitle>
        <Publisher>General Non-Fiction</Publisher>
        <Imprint>Macmillan</Imprint>
        <PublicationDate>07/04/2011</PublicationDate>
        <Contributors>
          <Contributor Code="A01" Text="By (author)">Cairns, Warwick</Contributor>
        </Contributors>
        <BicSubjects>
          <Bic Code="PDZ">Popular science</Bic>
        </BicSubjects>
        <Formats>
          <Format Type="6" Price="7.00" MaxDownloads="3" Status="active" ExclusiveRights="CA AU NZ ZA GB IE " NotForSale="AU NZ " />
        </Formats>
        <ShortDescription>     A serious and seriously funny book about weights and measures. It explains what they are, how they come about and how they are formed and shaped by the one guiding principle of measurement that no one ever mentions: that most of us have better things to think about. This is the only book devoted to the mishmash of bodges, estimates   </ShortDescription>
        <PhysicalEditionEan>9780330450300</PhysicalEditionEan>
      </EBook>
    </Extract>

到目前为止,我的代码可以获得所需的值。属性没有通过:(

$z = new XMLReader;
$z->open('files/eBiblio.xml');

$doc = new DOMDocument;

# move to the first node
while ($z->read() && $z->name !== 'EBook');

# now that we're at the right depth, hop to the next <product/> until the end of the tree
while ($z->name === 'EBook')
{

    $node = simplexml_import_dom($doc->importNode($z->expand(), true));

    # Get the value of each node
    $title = mysql_real_escape_string($node->Title);
    $Subtitle = mysql_real_escape_string($node->SubTitle);
    $ShortDescription = mysql_real_escape_string($node->ShortDescription);
    $Publisher = mysql_real_escape_string($node->Publisher);
    $Imprint = mysql_real_escape_string($node->Imprint);

    # Get attributes
    $isbn = $z->getAttribute('EAN');

    $contributor = $node->Contributors;
    $author = $contributor[0]->Contributor;
    $author = mysql_real_escape_string($author);

    $BicSubjects = $node->BicSubjects;
    $Bic = $BicSubjects[0]->Bic;
    $Bic = mysql_real_escape_string($Bic);


    $formats = $node->Formats;
    $type  = $formats[0]->Format;
    $price = $type[0]['Price'];
    $ExclusiveRights = $type[0]['ExclusiveRights'];
    $NotForSale = $type[0]['NotForSale'];

    # echo "<pre>".print_r($node,true)."</pre>";
    # die();

【问题讨论】:

  • 在此处添加您的 php 编码
  • 请看我下面的代码。我把它贴在这里的时候一团糟

标签: php xml simplexml xmlreader


【解决方案1】:

看看SimpleXML。特别是指向attributes() 的链接。这应该可以帮助您入门。

如果您仍有问题,请附上您正在使用的 php 代码。

编辑:

正如 Sundar 所说,简单 xml 更易于使用,但是如果您的 xml 文件很大,那么 XMLReader 和 simplexml 的组合将使用更少的内存,因为只存储当前节点。

while ($z->name === 'EBook')
{
  $node = simplexml_import_dom($doc->importNode($z->expand(), true));
  #print EAN
  echo $node->attributes()->EAN;

  ...

  $z->next('EBook');
}

how to use xmlreader in php

【讨论】:

  • 您先生,是我的英雄!谢谢你太棒了!
  • 我也设法让它像这样工作 $BicSubjects = $node->BicSubjects; $Bic = $BicSubjects[0]->Bic; $bicCode = $Bic[0]['代码'];
【解决方案2】:

//这可能对你有帮助

<?php

$cxn = mysql_connect('localhost', 'XXXX', 'XXXXXXXXX');

$xml = simplexml_load_file('graph.xml');

//print_r($xml);

//$ebook = $xml->EBook;

//get attribute
//echo($ebook->attributes()->EAN);

//get Contributors value
//echo $ebook->Contributors->Contributor;

foreach($xml->EBook as $ebook)
{

   //echo $ebook->Contributors->Contributor;

    $title = mysql_real_escape_string($ebook->Title);
    $Subtitle = mysql_real_escape_string($ebook->SubTitle);
    $ShortDescription = mysql_real_escape_string($ebook->ShortDescription);
    $Publisher = mysql_real_escape_string($ebook->Publisher);
    $Imprint = mysql_real_escape_string($ebook->Imprint);

    # Get attributes
    $isbn = $ebook->attributes()->EAN;

    $contributor = $ebook->Contributors;
    $author = $ebook->Contributors->Contributor;
    $author = mysql_real_escape_string($author);

    foreach($xml->EBook->Contributors->children() as $data)
    {

        $Code = $data->attributes()->Code;
        $Text = $data->attributes()->Code;
        /*
         print_r($data);
        die(); */
    }


    $BicSubjects = $ebook->BicSubjects;
    $Bic = $ebook->BicSubjects->Bic;
    $Bic = mysql_real_escape_string($Bic);


    $formats = $ebook->Formats;


    foreach($ebook->Formats->Format->children() as $child)
    {
        $type  = $child->attributes()->Type;
        $price = $child->attributes()->Price;
        $ExclusiveRights = $child->attributes()->ExclusiveRights;
        $NotForSale = $child->attributes()->NotForSale;
    }

}

【讨论】:

  • graph.xml 是你的 xml 文件,删除调试函数注释 (print_r) 你会得到所有的值
  • 您好,谢谢。问题是我将处理 50 万个项目,所以我不确定简单的 XMl 是否可以处理?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-12
  • 2012-06-21
  • 1970-01-01
  • 1970-01-01
  • 2016-10-18
  • 2017-05-30
相关资源
最近更新 更多