获取XMLReader的属性

朱利安

我在提取以下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”,也需要“凯恩斯,沃里克”。

任何帮助是极大的赞赏。

朱利安

下面的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();
詹姆斯·比克特(James Birkett)

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

如果您仍然有问题,请附上您正在使用的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');
}

查看如何在php中使用xmlreader

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

xmlreader,获取属性名称

来自分类Dev

使用XmlReader获取DTD文件的名称

来自分类Dev

C#xsd xmlreader,如何计算属性名称

来自分类Dev

使用XmlReader时,HasValue属性返回False!为什么?

来自分类Dev

如何使用XmlReader从XML中的特定子元素获取价值?

来自分类Dev

获取属性描述属性

来自分类Dev

获取属性的属性

来自分类Dev

获取附加属性的属性名称

来自分类Dev

Selenium 获取属性/CSS/属性

来自分类Dev

GhostDoc 如何获取属性的属性?

来自分类Dev

使用XMLReader在c#中解析XML以获取多个相似的节点

来自分类Dev

XmlReader获取当前文件路径的方法名称是什么?

来自分类Dev

如何使用xmlreader获取第一个找到的节点的内容?

来自分类Dev

XmlReader 在不向前移动光标的情况下获取值

来自分类Dev

使用 XmlReader 从 VB.NET 中的 XML 文件中获取名称和值

来自分类Dev

XMLReader错误

来自分类Dev

XMLReader错误

来自分类Dev

从样式属性获取高度

来自分类Dev

小票:获取标签属性

来自分类Dev

在LINQToXML中获取属性

来自分类Dev

获取父div的属性

来自分类Dev

无法获取属性“ addEventListener”

来自分类Dev

获取属性值BeautifulSoup

来自分类Dev

如何从对象获取属性?

来自分类Dev

获取单击元素的属性

来自分类Dev

Python:获取属性的价值

来自分类Dev

RESTful获取属性

来自分类Dev

获取覆盖的属性

来自分类Dev

获取样式的属性