我创建了一个simplexml php文件,它将加载我想要提取的所有xml项目。唯一的问题是有一个子项目位于具有属性的项目下。当我试图拉动儿童用品时,没有任何表现。我使用simplexml
还是比较新的XML
<Mediainfo version="0.7.62">
<File>
<track type="General">
<UniqueID_String>242652904449958064145306342749155800074 (0xB68D3FDDBE0F9B3E865F70325496B40A)</UniqueID_String>
<CompleteName>D:\Encoder\videos\raw\063 - 077 (Season_02)\064 - A Pirate-Loving Town Arrival at Whiskey Peak.mkv</CompleteName>
<Format>Matroska</Format>
<Format_Version>Version 1</Format_Version>
<FileSize_String>40.0 MiB</FileSize_String>
<Duration_String>24mn 6s</Duration_String>
<OverallBitRate_String>232 Kbps</OverallBitRate_String>
<Encoded_Date>UTC 2008-08-26 15:24:58</Encoded_Date>
<Encoded_Application>mkvmerge v2.2.0 ('Turn It On Again') built on Mar 4 2008 12:58:26</Encoded_Application>
<Encoded_Library_String>libebml v0.7.7 + libmatroska v0.8.1</Encoded_Library_String>
</track>
</File>
</Mediainfo>
我想抓住
<UniqueID_String>242652904449958064145306342749155800074 (0xB68D3FDDBE0F9B3E865F70325496B40A)</UniqueID_String>
下的
<track type="General">
PHP
$lib = simplexml_load_file("media.xml");
$xml = $lib->File;
$gen = $xml->track['General'];
$vid = $xml->track['Video'];
$aud = $xml->track['Audio'];
//General
$format = $gen->Format;
$app = $gen->Encoded_Application;
$size = $gen->FileSize_String;
$dur = $gen->Duration_String;
echo $format;
//Video
$vformat = $vid->Format;
$vbit = $vid->BitRate_Nominal_String;
$width = $vid->Width_String;
$height = $vid->Height_String;
$aspect = $vid->DisplayAspectRatio_String;
$frame = $vid->FrameRate_String;
$encode = $vid->Encoded_Library_String;
$encodes = $vid->Encoded_Library_Settings;
//Audio
$aformat = $aud->Format;
$compress = $aud->Compression_Mode_String;
$lang = $aud->Language_String;
答案 0 :(得分:1)
在SimpleXML中访问属性和子元素之间存在差异。
所以如果你想访问:
SimpleXML Basic Examples中列出了这一点。
在您的情况下,您想要访问孩子的孩子:
$xml = simplexml_load_file("data-15308758.xml");
### first track in first file ###
echo $xml->File->track->UniqueID_String, "\n";
### each track with attribute type="General" in each file ###
$count = 0;
foreach($xml->xpath('/*/File/track[@type="General"]') as $track)
{
echo ++$count, ': ', $track->UniqueID_String, "\n";
}
答案 1 :(得分:0)
此处的混淆是属性的名称和值之间。语法$xml->track['General']
检索名称为“General”的属性值,例如它会从<track General="hello" />
获得“你好”。
但是,在您的情况下,"General"
是名为type
:<track type="General">
的属性的值。因此,如果您想判断特定曲目是否为“常规”类型,则需要使用if ( $xml->track['type'] == 'General' )
。
我还假设您的文件中包含许多track
个元素,因此您需要循环遍历它们,就像它们在数组中一样:foreach ( $xml->track as $track )
;如果<File>
容器中有多个<MediaInfo>
元素,则需要两个嵌套循环。 (如果你不这样做,你将只看第一个<File>
和第一个<track>
。)
因此,在您的示例中显示的三种“类型”中,您的代码最终应该是这样的:
$lib = simplexml_load_file("media.xml");
foreach ( $lib->File as $file )
{
foreach ( $file->track as $track )
{
switch ( (string)$track['type'] )
{
case 'General':
$format = $gen->Format;
$app = $gen->Encoded_Application;
$size = $gen->FileSize_String;
$dur = $gen->Duration_String;
break;
case 'Video':
$vformat = $vid->Format;
$vbit = $vid->BitRate_Nominal_String;
$width = $vid->Width_String;
$height = $vid->Height_String;
$aspect = $vid->DisplayAspectRatio_String;
$frame = $vid->FrameRate_String;
$encode = $vid->Encoded_Library_String;
$encodes = $vid->Encoded_Library_Settings;
break;
case 'Audio':
$aformat = $aud->Format;
$compress = $aud->Compression_Mode_String;
$lang = $aud->Language_String;
break;
}
}
}
答案 2 :(得分:-1)
试试这个
$doc = new DOMDocument();
$doc->load('media.xml');
$xpd = new DOMXPath($doc);
false&&$node = new DOMElement();
$result = $xpd->query('//File/track[@type="General"]/UniqueID_String'); // your path
foreach($result as $node){
echo $node->nodeName ." => ". $node->nodeValue ."<br/>";
}