如何从 XML 文件中提取所有标签及其子项?
Posted
技术标签:
【中文标题】如何从 XML 文件中提取所有标签及其子项?【英文标题】:How can I pull all tags and their children from an XML file? 【发布时间】:2012-04-22 03:54:04 【问题描述】:XML 数据如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<xmldata>
<Orders>
<OrderID>2119</OrderID>
<AddressValidated>Y</AddressValidated>
<Affiliate_Commissionable_Value>2.0000</Affiliate_Commissionable_Value>
<AuthHash>d995fd9424fb99e80f58841a0ff2340f</AuthHash>
<AVS />
<BillingAddress1>122 27th West</BillingAddress1>
<BillingAddress2>Floor 11</BillingAddress2>
<BillingCity>New York</BillingCity>
<BillingCompanyName />
<BillingCountry>United States</BillingCountry>
<BillingFaxNumber />
<BillingFirstName>Dan</BillingFirstName>
<BillingLastName>Test</BillingLastName>
<BillingPhoneNumber>646456777</BillingPhoneNumber>
<BillingPostalCode>10001</BillingPostalCode>
<BillingState>NY</BillingState>
<CardHoldersName />
<CC_Last4 />
<CreditCardAuthorizationDate>3/30/2012 11:52:00 AM</CreditCardAuthorizationDate>
<CreditCardAuthorizationNumber>MANUAL</CreditCardAuthorizationNumber>
<CreditCardTransactionID />
<Customer_IPAddress>184.56.49.45</Customer_IPAddress>
<CustomerID>19</CustomerID>
<CVV2_Response />
<GiftWrapNote />
<IsAGift>N</IsAGift>
<LastModBy>7</LastModBy>
<LastModified>3/30/2012 1:39:00 PM</LastModified>
<Locked>Y</Locked>
<Order_Comments>Test sale - Don't process
Ignore statuses on this order</Order_Comments>
<Order_Entry_System>PHONE</Order_Entry_System>
<OrderDate>3/30/2012 11:01:00 AM</OrderDate>
<OrderNotes />
<OrderStatus>Ready to Ship</OrderStatus>
<PaymentAmount>8.1600</PaymentAmount>
<PaymentMethodID>17</PaymentMethodID>
<PONum />
<Processed_AutoEvents>Y</Processed_AutoEvents>
<SalesRep_CustomerID>7</SalesRep_CustomerID>
<SalesTax1>0.1700</SalesTax1>
<SalesTax2>0.0000</SalesTax2>
<SalesTax3>0.0000</SalesTax3>
<SalesTaxRate>0</SalesTaxRate>
<SalesTaxRate1>0.08375</SalesTaxRate1>
<SalesTaxRate2>0</SalesTaxRate2>
<SalesTaxRate3>0</SalesTaxRate3>
<ShipAddress1>123 27th West</ShipAddress1>
<ShipAddress2>Floor 1</ShipAddress2>
<ShipCity>New York</ShipCity>
<ShipCompanyName />
<ShipCountry>United States</ShipCountry>
<ShipFaxNumber />
<ShipFirstName>Dan</ShipFirstName>
<ShipLastName>Test</ShipLastName>
<ShipPhoneNumber>646782342</ShipPhoneNumber>
<Shipping_Locked>Y</Shipping_Locked>
<ShippingMethodID>1</ShippingMethodID>
<ShipPostalCode>10001</ShipPostalCode>
<ShipResidential>Y</ShipResidential>
<ShipState>NY</ShipState>
<Stock_Priority>3</Stock_Priority>
<Tax1_IgnoreNoTaxRules />
<Tax1_Title>Tax (8.375%)</Tax1_Title>
<Tax2_IgnoreNoTaxRules />
<Tax2_IncludePrevious>0</Tax2_IncludePrevious>
<Tax2_Title />
<Tax3_IgnoreNoTaxRules />
<Tax3_IncludePrevious>0</Tax3_IncludePrevious>
<Tax3_Title />
<Total_Payment_Authorized>0.0000</Total_Payment_Authorized>
<Total_Payment_Received>7.0700</Total_Payment_Received>
<TotalShippingCost>5.9900</TotalShippingCost>
<VendorID>0</VendorID>
<OrderDetails>
<OrderDetailID>3374</OrderDetailID>
<AutoDropShip>Y</AutoDropShip>
<FreeShippingItem>Y</FreeShippingItem>
<GiftTrakNumber>0</GiftTrakNumber>
<GiftWrap />
<GiftWrapCost>0.0000</GiftWrapCost>
<GiftWrapNote />
<IsKitID />
<KitID />
<Locked>Y</Locked>
<OnOrder_Qty>0</OnOrder_Qty>
<OptionIDs />
<Options />
<OrderID>2119</OrderID>
<ProductCode>test-product</ProductCode>
<ProductID>39200</ProductID>
<ProductName>Test Product 1</ProductName>
<ProductNote />
<ProductPrice>1.0000</ProductPrice>
<ProductWeight>1</ProductWeight>
<QtyOnBackOrder>0</QtyOnBackOrder>
<QtyOnHold>1</QtyOnHold>
<QtyShipped>0</QtyShipped>
<Quantity>1</Quantity>
<TaxableProduct>Y</TaxableProduct>
<TotalPrice>1.0000</TotalPrice>
<Vendor_Price>0.0000</Vendor_Price>
<Warehouses />
</OrderDetails>
<OrderDetails>
<OrderDetailID>3386</OrderDetailID>
<AutoDropShip />
<FreeShippingItem>Y</FreeShippingItem>
<GiftTrakNumber>0</GiftTrakNumber>
<GiftWrap />
<GiftWrapCost>0.0000</GiftWrapCost>
<GiftWrapNote />
<Locked>Y</Locked>
<OnOrder_Qty>0</OnOrder_Qty>
<Options />
<OrderID>2119</OrderID>
<ProductCode>test-product</ProductCode>
<ProductID>39200</ProductID>
<ProductName>Test Product 2</ProductName>
<ProductNote />
<ProductPrice>1.0000</ProductPrice>
<ProductWeight>1</ProductWeight>
<QtyOnBackOrder>0</QtyOnBackOrder>
<QtyOnHold>1</QtyOnHold>
<QtyShipped>0</QtyShipped>
<Quantity>1</Quantity>
<TaxableProduct>Y</TaxableProduct>
<TotalPrice>1.0000</TotalPrice>
</OrderDetails>
</Orders>
<Orders>
<OrderID>2141</OrderID>
<AddressValidated>Y</AddressValidated>
<Affiliate_Commissionable_Value>1.0000</Affiliate_Commissionable_Value>
<AuthHash>14cca8be8db44ca9943f0a6cbf592c83</AuthHash>
<AVS />
<BillingAddress1>122 27th West2</BillingAddress1>
<BillingAddress2 />
<BillingCity>New York2</BillingCity>
<BillingCompanyName />
<BillingCountry>United States</BillingCountry>
<BillingFaxNumber />
<BillingFirstName>Dan2</BillingFirstName>
<BillingLastName>Test2</BillingLastName>
<BillingPhoneNumber>646346322</BillingPhoneNumber>
<BillingPostalCode>10002</BillingPostalCode>
<BillingState>NY</BillingState>
<CardHoldersName />
<CC_Last4 />
<CreditCardAuthorizationDate>3/30/2012 3:33:00 PM</CreditCardAuthorizationDate>
<CreditCardAuthorizationNumber>MANUAL</CreditCardAuthorizationNumber>
<CreditCardTransactionID />
<Customer_IPAddress>56.75.23.34</Customer_IPAddress>
<CustomerID>19</CustomerID>
<CVV2_Response />
<GiftWrapNote />
<IsAGift>N</IsAGift>
<LastModBy>7</LastModBy>
<LastModified>3/30/2012 3:33:00 PM</LastModified>
<Locked>Y</Locked>
<Order_Comments>Test sale #2, don't process, ignore status.</Order_Comments>
<Order_Entry_System>PHONE</Order_Entry_System>
<OrderDate>3/30/2012 3:32:00 PM</OrderDate>
<OrderNotes />
<OrderStatus>Ready to Ship</OrderStatus>
<PaymentAmount>7.0700</PaymentAmount>
<PaymentMethodID>17</PaymentMethodID>
<PONum />
<Processed_AutoEvents>Y</Processed_AutoEvents>
<SalesRep_CustomerID>7</SalesRep_CustomerID>
<SalesTax1>0.0800</SalesTax1>
<SalesTax2>0.0000</SalesTax2>
<SalesTax3>0.0000</SalesTax3>
<SalesTaxRate>0</SalesTaxRate>
<SalesTaxRate1>0.08375</SalesTaxRate1>
<SalesTaxRate2>0</SalesTaxRate2>
<SalesTaxRate3>0</SalesTaxRate3>
<ShipAddress1>123 27th West2</ShipAddress1>
<ShipAddress2 />
<ShipCity>New York2</ShipCity>
<ShipCompanyName />
<ShipCountry>United States</ShipCountry>
<ShipFaxNumber />
<ShipFirstName>Dan2</ShipFirstName>
<ShipLastName>Test2</ShipLastName>
<ShipPhoneNumber>6461234789</ShipPhoneNumber>
<Shipping_Locked>Y</Shipping_Locked>
<ShippingMethodID>1</ShippingMethodID>
<ShipPostalCode>10002</ShipPostalCode>
<ShipResidential>Y</ShipResidential>
<ShipState>NY</ShipState>
<Stock_Priority>3</Stock_Priority>
<Tax1_IgnoreNoTaxRules />
<Tax1_Title>Tax (8.375%)</Tax1_Title>
<Tax2_IgnoreNoTaxRules />
<Tax2_IncludePrevious>0</Tax2_IncludePrevious>
<Tax2_Title />
<Tax3_IgnoreNoTaxRules />
<Tax3_IncludePrevious>0</Tax3_IncludePrevious>
<Tax3_Title />
<Total_Payment_Authorized>0.0000</Total_Payment_Authorized>
<Total_Payment_Received>7.0700</Total_Payment_Received>
<TotalShippingCost>5.9900</TotalShippingCost>
<VendorID>0</VendorID>
<OrderDetails>
<OrderDetailID>3404</OrderDetailID>
<AutoDropShip>Y</AutoDropShip>
<FreeShippingItem>Y</FreeShippingItem>
<GiftTrakNumber>0</GiftTrakNumber>
<GiftWrap />
<GiftWrapCost>0.0000</GiftWrapCost>
<GiftWrapNote />
<IsKitID />
<KitID />
<Locked>Y</Locked>
<OnOrder_Qty>0</OnOrder_Qty>
<OptionIDs />
<Options />
<OrderID>2141</OrderID>
<ProductCode>test-product</ProductCode>
<ProductID>39200</ProductID>
<ProductName>Test Product</ProductName>
<ProductNote />
<ProductPrice>1.0000</ProductPrice>
<ProductWeight>1</ProductWeight>
<QtyOnBackOrder>0</QtyOnBackOrder>
<QtyOnHold>1</QtyOnHold>
<QtyShipped>0</QtyShipped>
<Quantity>1</Quantity>
<TaxableProduct>Y</TaxableProduct>
<TotalPrice>1.0000</TotalPrice>
<Vendor_Price>0.0000</Vendor_Price>
<Warehouses />
</OrderDetails>
</Orders>
</xmldata>
我只需要所有<OrderDetails>
的孩子
在测试中,我尝试简单地回显所有数据,然后设置变量。
function processxmldetails()
$filexml='vl_rts.xml';
if (file_exists($filexml))
$xml = simplexml_load_file($filexml);
foreach ($xml->Orders->OrderDetails->children() as $Orders)
echo $Orders . '<br>';
//end foreach
这仅与此相呼应:
3374
Y
Y
0
0.0000
Y
0
2119
test-product
39200
Test Product 1
1.0000
1
0
1
0
1
Y
1.0000
0.0000
我需要每个<OrderDetails>
真的在它自己的变量中,所以我可以检查它是哪个<OrderDetailID>
并相应地插入另一个数据库。
我怎样才能一个接一个地调用这些<OrderDetail>
标签,然后检查它们的<OrderDetailID>
是什么来执行任务,然后转到下一个<OrderDetail>
?
【问题讨论】:
【参考方案1】:你可以用这个:
<?php
function processxmldetails()
$filexml = 'vl_rts.xml';
$data = array();
if( file_exists( $filexml ) )
$xml = simplexml_load_file( $filexml );
$i = 0;
foreach( $xml->Orders as $key => $orders )
$j = 0;
$data[ $i ] = array();
$data[ $i ][ 'OrderID' ] = ( int ) $orders->OrderID;
$data[ $i ][ 'OrderDetails' ] = array();
foreach( $orders->OrderDetails as $key1 => $value )
foreach( $value as $k => $v )
$data[ $i ][ 'OrderDetails' ][$j][ $k ] = ( string ) $v;
$j++;
$i++;
return $data;
$result = processxmldetails();
foreach( $result as $key => $value )
foreach( $value[ 'OrderDetails' ] as $k => $v )
foreach( $v as $k1 => $v1 )
echo $k1 .' = ' . $v1 . '<br />';
echo '----<br /><br />----';
?>
OrderDetailID
标记内的所有数据现在都在数组中。您可以根据自己的要求使用它。希望这会有所帮助。
【讨论】:
好的,太棒了,谢谢。你能告诉我如何读取数组吗?就像我如何调用第一个 OrderDetails 第 1 行,然后是第 2 行等...然后是第二个 OrderDetails 第 1 行,第 2 行等...?非常感谢。 我想我几乎明白了。所以像 $data[0][OrderID] 是第一个 OrderID 号,$data[2][OrderID] 是第二个 ID 号。我很难弄清楚如何调用其余部分。我知道,新手问题......对不起。 你是最棒的,非常感谢!我需要 $data[0][OrderDetails][0][OrderDetailID] 并增加它们以插入它们。那一点,调用你上面写的代码,让我头疼不已。我能看到光!哇!【参考方案2】:好吧,因为我不能假设这是我想出的 这是我们作为示例的 XML 文件
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
这是我们将用来提取信息的代码:)
$xml = simplexml_load_file("test.xml");
echo $xml->getName() . "<br />";
foreach($xml->children() as $child)
echo $child->getName() . ": " . $child . "<br />";
这将是我们的输出
note
to: Tove
from: Jani
heading: Reminder
body: Don't forget me this weekend!
--旧代码--我知道这是错误的语言我的错误-- 使用 get child,您可以从每个元素中提取您需要的信息
xml.getChild(index)
xml.getChild(path)
http://processing.org/reference/XMLElement_getChild_.html
祝你好运:)
【讨论】:
非常感谢您的回答。我可以麻烦您举个简单的例子吗?我以前从未使用过。所以比如说我只想要第一个 OrderDetails,我想检查第一个 OrderDetailID 标签的编号,然后做一些花哨的事情,然后转到第二个 OrderDetails 并做同样的事情。非常感谢。 我将添加另一个答案,以便我可以为您破解代码 问题是,如果我要求太多,请告诉我,但我真的需要一个基于上述 XML 示例的示例。注意一个以上是关于如何从 XML 文件中提取所有标签及其子项?的主要内容,如果未能解决你的问题,请参考以下文章
如何从 XML 中仅提取标签名称(而不是值),最终使用 XPath
Win 7 如何批量提取指定文件夹下的所有文件名称及其路径?
如何使用 PHP 从 XML“链接”标签中提取“href”属性?