帮酷LOGO
0 0 评论
  • 显示原文与译文双语对照的内容
文章标签:PHP  

介绍

我不得不在当前工作的项目中使用MySQL存储一些数据,因为我知道MySQL无法处理。 我想到的唯一想法是使用共享内存和 XML。

以前我在PHP上使用过 XML,所以我的第一React是从PHP手册中获取一些信息,但是viola非常少。 谷歌也没有帮助。

几个小时后,我把它们都working了,所以为了节省 forks,我决定分享我的经验。 本文将帮助你

  • 从头开始编写 XML
  • 从XML读取数据
  • 将节点添加到现有 XML
  • 修改现有XML中的节点
  • 从现有XML中删除节点

你只要拿起样本代码就可以了

从头开始编写 XML ( 使用 XMLWriter )

XMLWriter的目的是从头编写格式良好的XML。 它的重要方法之一是 startDocument(),它允许你声明使用( 虽然这些是)的版本和编码。

用法

要编写这里特性的简单 XML,请执行以下操作:
<?xmlversion="1.0"?><Customer><id>1</id><name>Oluwafemi</name><address>Cresent Drive, TX</address></Customer>
简单的例子。

这可以通过XMLWriter实现:

<?php//XMLwriter to write XML from scratch - Simple example$xml = new XMLWriter(); //create a new xmlwriter object$xml->openMemory(); //using memory for string output$xml->startDocument(); //create the document tag, you can specify the version and encoding here$xml->startElement("Customer"); //Create an element$xml->writeElement("id", "1");
$xml->writeElement("name", "Oluwafemi"); //Write to the element$xml->writeElement("address", "Cresent Drive, TX");
$xml->endElement(); //End the elementecho $xml->outputMemory(); //done with writing output the xml?>

这在下载过程中命名为"简单- xmlwriter.php"

一个复杂的例子是编写这个 XML:

<xmlversion="1.0"?><purchase><customer><id>1</id><time>2013-04-19 10:56:03</time><total>$350</total></customer><customer><id>2</id><time>2013-04-23 13:43:41</time><total>$1456</total></customer></purchase>
小复杂的例子。

这段代码解决了难题:

<?php//XMLwriter to write XML from scratch - Little complex example$xml = new XMLWriter();
$xml->openMemory();
$xml->startDocument();
$xml->startElement("purchase");
$xml->startElement("customer"); //Start of customer with id 1$xml->writeElement("id", 1);
$xml->writeElement("time", "2013-04-19 10:56:03");
$xml->writeElement("total", "$350");
$xml->endElement(); //End of customer with id 1$xml->startElement("customer"); //Start of customer with id 2$xml->writeElement("id", 2);
$xml->writeElement("time", "2013-04-23 13:43:41");
$xml->writeElement("total", "$1456");
$xml->endElement(); //End of customer with id 1$xml->endElement();echo $xml->outputMemory();?>

这在下载过程中命名为"小复杂- xmlwriter.php"

更复杂的是向某些节点提供属性,例如本示例中的<product> 节点:

<?xmlversion="1.0"?><products><productpid="314"><name>Apple</name><price>$1.00</price><discount>3%</discount></product><productpid="315"><name>Mango</name><price>$0.90</price><discount>3%</discount></product></products>
更复杂的示例( 这里XML将在以下各种示例中使用)。

这也可以通过XMLWriter实现:

<?php//XMLwriter to write XML from scratch - Little complex example$xml = new XMLWriter();
 $xml->openMemory();
 $xml->startDocument();
 $xml->startElement("products");
 $xml->startElement("product"); //Start of product with pid 314 $xml->writeAttribute("pid", 314);
 $xml->writeElement("name", "Apple");
 $xml->writeElement("price", "$1.00");
 $xml->writeElement("discount", "3%");
 $xml->endElement(); //End of product with pid 314 $xml->startElement("product"); //Start of product with pid 315 $xml->writeAttribute("pid", 315);
 $xml->writeElement("name", "Mango");
 $xml->writeElement("price", "$0.90");
 $xml->writeElement("discount", "3%");
 $xml->endElement(); //End of product with pid 315 $xml->endElement();
 echo $xml->outputMemory(); //Done with writing, output the xml?>
这在下载过程中命名为"更复杂- xmlwriter.php"

从XML读取/获取数据( 使用XMLReader和 SimpleXMLElement )

XMLReader用于从整个XML中调用"节点 XML"。 通过使用(。文件,URL,等等 ) 方法或者使用 xml() 方法,可以从URI中使用 open() 方法加载 XML。 本插图将使用 xml() 方法全部通过。 对于 open() 方法来说,只需提供包含 XML 比如。open("c:/.../this.xml"); 或者的URI open("http://example.com/this.xml");

SimpleXMLElement可以用于从 XMLReader 获取的"节点 xml"中提取数据。

获取用户在上面的小复杂例子中为 1的客户所花费的总金额

<?php.
.
.
 /* XMLReader starts from here for Little Complex Example - XMLReader
 To get the total amount spent by custormer with id 1
 */$rxml = new XMLReader(); //Create new XMLReader Object $rxml->xml($nXML); //Load in the XML while($rxml->read() && $rxml->name!== 'customer');
 //Move to the first customer child $amountSpent = 0;
 while($rxml->name === 'customer')
 {
 /* The child xml gotten using the readOuterXML() will look thus
 <customer><id>1</id><time>2013-04-19 10:56:03</time><total>$350</total></customer>
 *///Read the current child xml into a SimpleXMLElement$node = new SimpleXMLElement($rxml->readOuterXML());
 if($node->id == 1)
 //Check if the id node as 1 as it value {
 $amountSpent = $node->total;
 break;
 }
 $rxml->next('customer');
 //Move to the next customer child }
 echo$amountSpent;?>
这在下载过程中命名为"小复杂示例- xmlreader.php"

并从上更复杂的例子中获得产品的名称。价格和折扣

/* XMLReader starts from here for More Complex XMLReader
 To get the name, price, and discount of the prouct with attribute pid = 315
 */$rxml = new XMLReader(); //Create new XMLReader Object $rxml->xml($nXML); //Load in the XML while($rxml->read() && $rxml->name!== 'product'); //Move to the first product child $name = "";
 $price = "";
 $discount = "";
 while($rxml->name === 'product')
 { /* The child xml gotten using the readOuterXML() will look thus
 <product pid="314"><name>Apple</name><price>$1.00</price><discount>3%</discount></product>
 */ if($rxml->getAttribute("pid") == "315") //Check if the atrribute pid is equals to 315 {
 $node = new SimpleXMLElement($rxml->readOuterXML());//Read the current child xml into a SimpleXMLElement$name = $node->name;
 $price = $node->price;
 $discount = $node->discount;
 break;
 }
 $rxml->next('product'); //Move to the next product child }
 echo"The product is {$name} and is {$price} with {$discount} as discount";?>
这在下载过程中命名为"更复杂- xmlreader.php"

向已经存在的XML添加子级( 使用 SimpleXMLElement )

XMLWriter旨在从开始到结束编写新鲜的XML。 但是要将一个新产品添加到上面的XML更复杂的例子中,你不希望重写整个 XML。 ( 假设现在的XML有 350,000个产品节点 https://www.codeproject.com/script/Forums/Images/smiley_wink.gif )。 SimpleXMLElement可以用于向已经存在的XML中添加额外的节点。 要添加价格 $3.00,折扣 0.3 % 和 pid 328的橙色,这里代码适用
<?php.
.
.//Adding node starts from here$sXML = new SimpleXMLElement($nXML); //Load the entire xml in$newchild = $sXML->addChild("product");//Notice am now using the $newchild object not the $sXML object$newchild->addAttribute("pid", 328);
$newchild->addChild("name", "Orange");
$newchild->addChild("price", "$3.00");
$newchild->addChild("discount", "0.3%");echo $sXML->asXML();//Notice am now back to the $sXML object. The asXML() method is used to output the XML ?>
这在下载过程中命名为"添加 node.php"

修改现有XML中的节点( 使用DomDocument和 DOMXPath )

说苹果的价格从 $1.00改为 $2.00 ( 我希望苹果iPad是如何销售的,或者它们不是苹果的),折扣下降到 1%。 然后可以使用 DomDocument 修改 XML,同时使用 DOMXPath 查找要修改的节点。

<?php.
.
.
 //Modifying Node starts from here// Create a new document fragment to hold the new <product> node$productId = 314;
 $parent = new DomDocument;
 $parent_node = $parent->createElement('product');
 //Add the pid attribute//Notice that the createAttribute() method was used on $parent$attribute = $parent->createAttribute("pid");
 //but appended to $parent_node $attribute->value = $productId;
 $parent_node->appendChild($attribute);
 // Add the child elements $parent_node->appendChild($parent->createElement('name', "Apple"));
 $parent_node->appendChild($parent->createElement('price', "$2.00"));
 $parent_node->appendChild($parent->createElement('discount', "1%"));
 //Append the $parent_node into $parent $parent->appendChild($parent_node);
 // Load the original XML to modify into a new DomDocument$dom = new DomDocument;
 $dom->loadXML($nXML);
 // Locate the old product node to modify$xpath = new DOMXpath($dom);
 $nodelist = $xpath->query("/products/product[@pid={$productId}]");
 $oldnode = $nodelist->item(0);
 // Load the $parent document fragment into the $dom$newnode = $dom->importNode($parent->documentElement, true);
 // Replace $oldnode->parentNode->replaceChild($newnode, $oldnode);
 echo $dom->saveXML();
 //Done output XML ?></product>
这在下载过程中命名为"修改 node.php"

删除现有XML中的节点( 使用DomDocument和 DOMXPath )

假设我们不再销售芒果,我们必须从XML中删除它。

<?php.
.
.//Deleting Node of Mango//Load the original XML to modify into a new DomDocument$productId = 315;$dom = new DomDocument;
$dom->loadXML($nXML);// Locate the old product node to modify$xpath = new DOMXpath($dom);$nodelist = $xpath->query("/products/product[@pid={$productId}]");$oldnode = $nodelist->item(0);// Remove$oldnode->parentNode->removeChild($oldnode);echo $dom->saveXML();//Done output XML ?>

这在下载过程中命名为"删除 node.php"



文章标签:PHP  

Copyright © 2011 HelpLib All rights reserved.    知识分享协议 京ICP备05059198号-3  |  如果智培  |  酷兔英语