0

这是我的原始 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>"; 

?>

这是此代码之后的输出:(由制造商ID选择的记录)

<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>

它如何显示我想要的记录的全部内容是这样的,所以我可以根据我的需要进行格式化。元价格描述中的关键字和由购买网址链接的照片,例如<a href="$buyurl"><img src="$image_url"></a>

4

1 回答 1

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>";
于 2013-03-27T23:21:29.020 回答