使用php从xml文件中选择Attributes

时间:2013-03-27 00:24:27

标签: php sql xml

这是我原来的XML,我想在它上面运行sql,所以我可以从中选择一条记录但是,我已经成功完成了,我想只得到我想要的记录

<catalog>
<product>
<programname>Teva Footwear</programname>
<programurl>http://www.teva.com?cid=aff_pr</programurl>
<catalogname>Teva Footwear Product Catalog</catalogname>
<lastupdated>03/25/2013</lastupdated>
<name>Teva Men Tanza Leather Sport Sandals in Brown</name>
<keywords>Teva Men Tanza Leather Sport Sandals in Brown</keywords>
<description>...</description>
<sku>1000183-BRN</sku>
<manufacturer>Teva</manufacturer>
<manufacturerid>1000183-BRN</manufacturerid>
<currency>USD</currency>
<saleprice>85.00</saleprice>
<price>85.00</price>
<retailprice>85.00</retailprice>
<fromprice>Yes</fromprice>
<buyurl>...</buyurl>
<impressionurl>http://www.awltovhc.com/image-6059025-10757671</impressionurl>
<imageurl>...</imageurl>
<advertisercategory>Men</advertisercategory>
<promotionaltext>Fast, Free Shipping and Free Returns!</promotionaltext>
<online>Yes</online>
<instock>YES</instock>
<condition>New</condition>
<warranty>...</warranty>
<standardshippingcost>0.0</standardshippingcost>
</product>
<product>...</product>
<product>
<programname>Teva Footwear</programname>
<programurl>http://www.teva.com?cid=aff_pr</programurl>
<catalogname>Teva Footwear Product Catalog</catalogname>
<lastupdated>03/25/2013</lastupdated>
<name>Teva Men Original Mush Flip Flops in Tread Black</name>
<keywords>Teva Men Original Mush Flip Flops in Tread Black</keywords>
<description>
See what all the hype is about and get yourself a pair of men's Teva Original Mush sandals today! Mush is the ultimate in flip flop foot-forming comfort. The Original Mush features a thicker sole and slightly wider forefoot and heel for a more generous fit.

这是我到目前为止所做的(当然我在网上找到了代码)

<?php 

// ----------------  The example uses the php 5 "DOM Functions"  to select specific node uding Xpath query ------------------  

$doc = new DOMDocument;                                        // Create a new dom document 
$doc->preserveWhiteSpace = false;                              // Set features 
$doc->formatOutput = true;                                     // Create indents on xml 

$doc->Load('Teva_Footwear-Teva_Footwear_Product_Catalog.xml');                                        // Load the file 

$xpath = new DOMXPath($doc); 
$query = '//catalog/product/sku[. = "1000183-WAL"]';         // The xpath (starts from root node) 
$names = $xpath->query($query);                                 // A list of matched elements 

$Output=""; 
foreach ($names as  $node) { 
   $Output.=$doc->saveXML($node->parentNode)."\n";             // We get the parent of  "<firstname>" element  (the entire "<person>" node and its children) (maybe get 

the parent node directly using xpath) 
                                                                // and use the saveXML() to convert it to string   
} 


echo $Output."<br>\n\n";                                      // The result  

echo "<hr><br><b>Below view the results  as HTML content. &nbsp; (See also the page's HTML code):</b>\n<pre>".htmlspecialchars($Output)."</pre>"; 

?>

在此代码之后输出:(由manufactrid选择的记录)

<product>
  <programname>Teva Footwear</programname>
  <programurl>http://www.teva.com?cid=aff_pr</programurl>
  <catalogname>Teva Footwear Product Catalog</catalogname>
  <lastupdated>03/25/2013</lastupdated>
  <name>Teva Men Tanza Leather Sport Sandals in Walnut</name>
  <keywords>Teva Men Tanza Leather Sport Sandals in Walnut</keywords>
  <description>Following in the footsteps of our other sports sandals (no pun intended)  the Tanza Leather was built to perform. Featuring our Universal Strapping System with 3 points of adjustment  as well as Shoc Pad&amp;trade; technology  the Tanza Leather gives you a luxurious feel without sacrificing functionality.</description>
  <sku>1000183-WAL</sku>
  <manufacturer>Teva</manufacturer>
  <manufacturerid>1000183-WAL</manufacturerid>
  <currency>USD</currency>
  <saleprice>85.00</saleprice>
  <price>85.00</price>
  <retailprice>85.00</retailprice>
  <fromprice>Yes</fromprice>
  <buyurl>http://www.dpbolvw.net/click-6059025-10757671?url=http%3A%2F%2Fwww.teva.com%2Fwomens-tanza-leather-feminine-sports-sandals%2F737872627089%252Cdefault%252Cpd.html</buyurl>
  <impressionurl>http://www.awltovhc.com/image-6059025-10757671</impressionurl>
  <imageurl>http://www.teva.com/on/demandware.static/Sites-TEVA-US-Site/Sites-masterCatalogTeva/default/v1364225639089/images/large/T0183-WAL_1.jpg</imageurl>
  <advertisercategory>Men</advertisercategory>
  <promotionaltext>Fast, Free Shipping and Free Returns!</promotionaltext>
  <online>Yes</online>
  <instock>YES</instock>
  <condition>New</condition>
  <warranty>Teva has a 1 year warranty against defective materials and/or workmanship</warranty>
  <standardshippingcost>0.0</standardshippingcost>
</product>

然而,为了那个记录显示整件事我想要的是这样的东西,所以我可以根据我的需要进行格式化。 meta中的关键字 价钱 描述 和照片通过购买网址链接,例如<a href="$buyurl"><img src="$image_url"></a>

1 个答案:

答案 0 :(得分:0)

使用simplexml的解决方案:

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

list($product) = $xml->xpath("//product[sku = '1000183-WAL']");

echo "<a href=\"{$product->imageurl}\"><img src=\"{$product->imageurl}\" /></a>";