【发布时间】:2017-06-30 23:10:06
【问题描述】:
我正在使用 PHP 的 simpleXml 将 musicxml 文件显示为屏幕上的乐谱。除非乐谱中某处出现谱号变化,否则一切都运行良好。这是 xml 文件的摘录,显示了小节 27 中的谱号变化:
<measure number="27">
<note>
</note>
<note>
</note>
<attributes>
<clef number="2">
<sign>F</sign>
<line>4</line>
</clef>
</attributes>
<note>
</note>
<note>
</note>
<note>
</note>
</measure>
我正在通过 foreach ($measure->note as $note) 浏览每个度量中的注释。我可以通过 if(isset($measure->attributes->clef)) 检测到小节中的谱号变化,但这并不能告诉我 在哪里谱号变化发生(在本例中,在小节的第二个音符之后和最后三个音符之前。)
我查看了 simpleXmlIterator,但它似乎无法处理 simpleXml 对象的摘录(在本例中为 $measure)。我试过了:
$sxi = new SimpleXMLIterator($measure);
这给了我以下警告:
警告: SimpleXMLElement::__construct():实体:第 29 行:解析器错误:开始 应有标记,未找到“
当我var_dump($measure) 时,我看到它将谱号更改放在音符数组的末尾,但为其分配了一个数字,可用于确定其在音符序列中的正确位置:
object(SimpleXMLElement)#21 (4) {
["@attributes"]=>
array(2) {
["number"]=>
string(2) "25"
["width"]=>
string(6) "468.94"
}
["note"]=>
array(25) {
...
[20]=>
object(SimpleXMLElement)#43 (5) {
["rest"]=>
object(SimpleXMLElement)#49 (0) {
}
["duration"]=>
string(1) "4"
}
[21]=>
object(SimpleXMLElement)#45 (7) {
["@attributes"]=>
array(2) {
["default-x"]=>
string(6) "293.75"
["default-y"]=>
string(7) "-255.00"
}
["pitch"]=>
object(SimpleXMLElement)#49 (2) {
["step"]=>
string(1) "D"
["octave"]=>
string(1) "4"
}
["duration"]=>
string(1) "2"
}
}
["attributes"]=>
object(SimpleXMLElement)#44 (1) {
["clef"]=>
object(SimpleXMLElement)#49 (3) {
["@attributes"]=>
array(1) {
["number"]=>
string(1) "2"
}
["sign"]=>
string(1) "G"
["line"]=>
string(1) "2"
}
}
}
对象#44(谱号变化)应位于对象#43 和#45 的两个音符[实际上是一个休止符和一个音符]之间。所以,如果我能找到一种访问这些“对象编号”的方法,我的问题就可以解决。有谁知道如何做到这一点,或者更好的方法来解决这个问题?
【问题讨论】: