0

Given a legacy input XML file of the form:

<?xml version="1.0" ?>
<data>
<map>
    <key>title</key>
    <string>A Title Here</string>

    <key>description</key>
    <string>Description goes here</string>

    <key>elements</key>

    <map>
      <key>item1</key>
      <map>
        <key>name</key>
        <string>foo</string>
        <key>url</key>
        <string>http://foo.com</string>
      </map>

      <key>item2</key>
      <map>
        <key>name</key>
        <string>bar</string>
        <key>url</key>
        <string>http://bar.com</string>
      </map>

      <key>item3</key>
      <map>
        <key>name</key>
        <string>flasm</string>
        <key>url</key>
        <string>http://flasm.com</string>
      </map>

    </map>
</map>

using Javascript, I want to find the 'elements' tag (not always in the same place) and then iterate over all its children ('item' N) and gather the key/string pairs for each (e.g. "name=foo")

I've tried to do it a number of ways using DOMReader and Xpath but have not been able to get it quite right.

Can anyone show me how best to do this?

(This doesn't look like other XML I've seen so maybe that's why I'm having trouble using this in the examples I've found but as I said, it's legacy and I have no control over it).

Many thanks.

2
  • This looks like an Apple configuration file for me. Starting at elements would be starting more or less in the middle of it. Are you sure you want to do this? A better way would be to start at the elements of a map called A Title Here. Commented Oct 17, 2014 at 7:11
  • No reason I couldn't start at the top and test if a key is 'elements' or not, then start iterating from there. Commented Oct 17, 2014 at 18:15

1 Answer 1

1

I'm not entirely sure what you mean by "not always in the same place", if you could elaborate on that I may be able to supply a better answer. But if "elements" and its children (in your example, 'ItemN' is not a child, but a sibling) can be nested differently, you could search by just the text content like so:

//*[contains(text(), 'elements')]/following-sibling::map/key[contains(text(), 'item1')]/following-sibling::map[1]

This would give you the pairs you're looking for, if the child nodes you're looking for are in fact the next direct sibling as in your example.

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

1 Comment

Thank you JWiley - I mean that 'elements' key doesn't necessarily follow the 'description' key. That seems to work well - 2 questions: 1/ I don't know how many items there are - can I get back an iterator over all that there are? 2/ What's the syntax for accesing a child of 'item n' - say another map or a value? Do I use another XPath statement based on the first iterator? <

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.