2012-09-07 51 views
0

我使用xml_grep從XML文檔中導出文本。xml_grep獲取第一個實例的值

所以我有一個XML文檔,像這樣:

<?xml version="1.0" encoding="UTF-8"?> 
<Mediainfo version="0.7.52"> 
<File> 
<track type="General"> 
<Complete_name>grm.mov</Complete_name> 
<Format>MPEG-4</Format> 
<Format_profile>QuickTime</Format_profile> 
<Codec_ID>qt </Codec_ID> 
<File_size>10.1 MiB</File_size> 
<Duration>3s 600ms</Duration> 
<Overall_bit_rate_mode>Variable</Overall_bit_rate_mode> 
<Overall_bit_rate>23.5 Mbps</Overall_bit_rate> 
<Encoded_date>UTC 2012-08-01 09:33:54</Encoded_date> 
<Tagged_date>UTC 2012-08-01 09:34:00</Tagged_date> 
<Writing_library>Apple QuickTime</Writing_library> 
<TIM>00:00:00:00</TIM> 
<TSC>25</TSC> 
<TSZ>1</TSZ> 
</track> 

<track type="Video"> 
<ID>1</ID> 
<Format>JPEG</Format> 
<Codec_ID>mjpa</Codec_ID> 
<Duration>3s 600ms</Duration> 
<Bit_rate_mode>Variable</Bit_rate_mode> 
<Bit_rate>23.5 Mbps</Bit_rate> 
<Width>1 920 pixels</Width> 
<Height>1 080 pixels</Height> 
<Display_aspect_ratio>16:9</Display_aspect_ratio> 
<Frame_rate_mode>Constant</Frame_rate_mode> 
<Frame_rate>25.000 fps</Frame_rate> 
<Scan_type>Interlaced</Scan_type> 
<Scan_order>Bottom Field First</Scan_order> 
<Compression_mode>Lossy</Compression_mode> 
<Bits__Pixel_Frame_>0.453</Bits__Pixel_Frame_> 
<Stream_size>10.1 MiB (100%)</Stream_size> 
<Language>English</Language> 
<Encoded_date>UTC 2012-08-01 09:33:54</Encoded_date> 
<Tagged_date>UTC 2012-08-01 09:34:00</Tagged_date> 
</track> 

<track type="Menu"> 
<ID>2</ID> 
<Language>English</Language> 
<Encoded_date>UTC 2012-08-01 09:34:00</Encoded_date> 
<Tagged_date>UTC 2012-08-01 09:34:00</Tagged_date> 
</track> 

</File> 
</Mediainfo> 

有一個名爲 '長' 2個元素。一個在<track type="General">下,另一個在<track type="Video">下。我怎樣才能分別導出每一個?

xml_grep 'Duration' 

返回這兩個元素。我知道這是一個不好的問題,但我無法弄清楚。

預先感謝您。

+0

正確的答案是:xml_grep --cond = '軌道[@類型= 「視頻」] /時間' – mindcast

回答

0

你schould而使用XPath

$value1 = $xml->xpath('/file/track/@Video/Duration'); 

$value2 = $xml->xpath('/file/track/@General/Duration'); 

只是一個想法。

或soemthing這樣的:

xml_grep -t '*[@attrib="pickme"]' *.xml or xml_grep2 -t '//*[@attrib="pickme"]' *.xml