0

I am trying to access some data that is many levels deep in the hierarchy of my XML file.

<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>

Here is my code were I am trying to access the data through the hierarchy.

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

I keep getting errors that it cant find that specific list of elements but I am certain that that is the hierarchy. I am wondering if fact that there are multiple levels of XML elements with the same name is causing the issue. Is it possible to grab my data between the two text brackets?

0

2 Answers 2

1

The solution is easy:

                          </scenenode>


                    </layer>

                    </layer>

            </layer    <!-- ① //-->
        </layer        <!-- ② //-->

    </scene>

</ozml>

You have two missing closing brackets (>) on the lines I've marked above as ① and ② . Your script works as it should.

Sign up to request clarification or add additional context in comments.

5 Comments

BTW That was a good catch, I looked at the OP for a few minutes and totally missed it! :) +1 The funny part though, I saw a difference in the code highlighting and it just didn't register, but it does now. I think I paid to much attention to the erratic use of excess white space. :)
that isnt the actual XML. I just typed out the hierarchy to the data i am trying to get. The actual XML has closed brackets. That still doesn't solve the issue of accessing multiple levels with the same name
@Nick I don't understand what your issue is: your script, when I tested it, worked fine. There is no issue with retrieving tags at different levels that have the same name. If you can be clearer about what your problem is, I can help you solve it. But, as it stands, I have not encountered a problem other than the one my answer pointed out, and accessing multiple levels of XML data is always just a matter of get XML element "bar" of XML element "bar" of XML element "foo" of XML element "bar" of....
Hmm I guess I am not too sure. Whenever i came across a layer that had multiple levels of the same name such as "layer" then my original script wouldn't work but the answer that i posted down below works perfectly.
@Nick Well, as I said, your script that I tested worked perfectly too, and coped with traversing the many tags called layer, and also in retrieving the values of multiple tags called text. Your workaround below is very inefficient, plus it’s almost unreadable with the huge number of nested statements. Anyway, if you’re happy the issue is solved, that’s probably what matters most.
0

I don't know if this is the best way to go about it but this is how I got it to work.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.