0

我创建了一个 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 (&apos;Turn It On Again&apos;) 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;
4

3 回答 3

1

在 SimpleXML 中访问属性子元素是有区别的。

因此,如果您想访问:

  • 子元素使用对象属性访问
  • 属性使用数组访问

这也在SimpleXML 基本示例中进行了概述。

在您的情况下,您想访问孩子的孩子:

$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";
}

http://eval.in/12273

于 2013-03-09T08:37:35.990 回答
0

这里的混淆在于属性的名称值之间。该语法$xml->track['General']检索名称为“General”的属性的值,例如,它将从 中获取“hello” <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;
        }
    }
}
于 2013-03-10T21:23:27.900 回答
-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/>";
}
于 2013-03-09T08:44:36.363 回答