通过Applescript访问具有相同名称的多个XML元素级别

时间:2018-02-19 22:46:13

标签: xml xml-parsing applescript

我正在尝试访问XML文件层次结构中深层次的一些数据。

<ozml>

    <scene>


        <layer name=" Master Project Group" id="3097594133">
            <layer name=" Focus Group" id="3097594131">
                <layer name="⏹ Title Group" id="3097594129">


                    <layer name=" Main Title Group" id="3111213570">


                        <scenenode name="Main Title" id="3097595437" factoryID="61" version="5">


                            <parameter name="Object" id="2" flags="8589938704">

                                <parameter name="Text" id="369" flags="8606777344">
                                    <text>DATA THAT I WANT</text>
                                </parameter>

                            </parameter>


                        </scenenode>




                    </layer>

                </layer>

            </layer
        </layer

    </scene>



</ozml>

这是我的代码,我试图通过层次结构访问数据。

tell application "System Events"
set theXMLFile to XML file myFile

set myData to (XML elements of XML element "parameter" of XML element "parameter" of XML element "scenenode" of XML element "layer" of XML element "layer" of XML element "layer" of XML element "layer" of XML element "scene" of XML element "ozml" of theXMLFile whose name is "text")
set textList to {}

repeat with i from 1 to (count myData)
    set end of textList to value of item i of myData

end repeat
end tell

display dialog textList as string

我一直在收到错误,因为它无法找到特定的元素列表,但我确信这就是层次结构。我想知道是否存在多个具有相同名称的XML元素级别导致该问题。是否可以在两个文本括号之间获取数据?

2 个答案:

答案 0 :(得分:1)

解决方案很简单:

1 2 3 4
2 4 some_value 6
4 5 6 7
3 4 5 6

您在上面标有①和②的行上有两个缺少右括号( </scenenode> </layer> </layer> </layer <!-- ① //--> </layer <!-- ② //--> </scene> </ozml> )。你的脚本可以正常工作。

答案 1 :(得分:0)

我不知道这是否是最好的方法,但这就是我开始工作的方式。

set myUUIDListNew to {}
tell application "System Events"
tell XML file myXMLFile
    tell XML element "scene" of XML element "ozml"
        set theFactoryElements to (every XML element whose name = "layer")


        repeat with a from 1 to length of theFactoryElements
            set theCurrentFactoryElement to item a of theFactoryElements

            tell theCurrentFactoryElement

                set theUUID to value of XML attribute "name"

                if theUUID = " Master Project Group" then

                    set secondLayer to (every XML element whose name is "layer")

                    repeat with a from 1 to length of secondLayer

                        set theCurrentLayerElement to item a of secondLayer

                        tell theCurrentLayerElement

                            set the2nd to value of XML attribute "name"

                            if the2nd = " Focus Group" then

                                set thirdLayer to (every XML element whose name is "layer")

                                repeat with a from 1 to length of thirdLayer

                                    set theThirdLayerElement to item a of thirdLayer

                                    tell theThirdLayerElement

                                        set the3rd to value of XML attribute "name"

                                        if the3rd = "⏹ Title Group" then

                                            set fourthLayer to (every XML element whose name is "layer")

                                            repeat with a from 1 to length of fourthLayer

                                                set theFourthLayerElement to item a of fourthLayer

                                                tell theFourthLayerElement

                                                    set the4th to value of XML attribute "name"

                                                    if the4th = " Main Title Group" then

                                                        set scenenodeLayer to (every XML element whose name is "scenenode")

                                                        repeat with a from 1 to length of scenenodeLayer

                                                            set thescenenodeElement to item a of scenenodeLayer

                                                            tell thescenenodeElement

                                                                set theSceneNode to value of XML attribute "name"

                                                                if theSceneNode = "Main Title" then

                                                                    set parameterLayer to (every XML element whose name is "parameter")

                                                                    repeat with a from 1 to length of parameterLayer

                                                                        set theParameterElement to item a of parameterLayer

                                                                        tell theParameterElement

                                                                            set param1 to value of XML attribute "name"

                                                                            if param1 = "Object" then

                                                                                set parameter1Layer to (every XML element whose name is "parameter")

                                                                                repeat with a from 1 to length of parameter1Layer

                                                                                    set theSecondParameterElement to item a of parameter1Layer

                                                                                    tell theSecondParameterElement

                                                                                        set param2 to value of XML attribute "name"

                                                                                        if param2 = "Text" then

                                                                                            set theBookName to value of XML element "text"

                                                                                            set end of myUUIDListNew to theBookName & return


                                                                                        end if

                                                                                    end tell
                                                                                end repeat
                                                                            end if

                                                                        end tell
                                                                    end repeat
                                                                end if

                                                            end tell
                                                        end repeat


                                                    end if

                                                end tell
                                            end repeat


                                        end if

                                    end tell
                                end repeat
                            end if
                        end tell
                    end repeat
                end if
            end tell
        end repeat
    end tell
end tell
end tell
display dialog myUUIDListNew as string