Search code examples
xmldartxpathnested

Create branches to children for nesting xml in dart?


I have following xml structure file

<?xml version="1.0" encoding="utf-8"?>

    <root uuid="{c6ede94d-c675-48ec-8a81-2080c6ea5725}"><title>Root</title>
        <folder uuid="{eae91fe9-469c-4743-9ab8-10cd65676c9b}"><title>Lv1</title>
            <folder uuid="{b495b5ab-e0cc-4a80-af80-f6fc6e9c902e}"><title>Lv2</title>
            </folder>
            <folder uuid="{df8ffcaf-e9ce-4632-a928-b930e3edd586}"><title>Lv2</title>
            </folder>
        </folder>
        <folder uuid="{2c0c0cd6-fb47-4dcd-973b-3af07eee7e7b}"><title>Lv1</title>
            <folder uuid="{d5518b4e-5f27-487d-bda9-36115e101cc9}"><title>Lv2</title>
            </folder>
            <folder uuid="{8f30601d-d3e7-42fc-a0ce-034d75124c33}"><title>Lv2</title>
            </folder>
        </folder>
    </root>

Then I need to create connecting list between uuid of father and children like this

branches =   {
    "branch": [
      {
        "Root": "{c6ede94d-c675-48ec-8a81-2080c6ea5725}",
        "Lv1": "{eae91fe9-469c-4743-9ab8-10cd65676c9b}"
      },
      {
        "Root": "{c6ede94d-c675-48ec-8a81-2080c6ea5725}",
        "Lv1": "{2c0c0cd6-fb47-4dcd-973b-3af07eee7e7b}"
      },
      {
        "Lv1": "{eae91fe9-469c-4743-9ab8-10cd65676c9b}",
        "Lv2": "{b495b5ab-e0cc-4a80-af80-f6fc6e9c902e}"
      },
      {
        "Lv1": "{eae91fe9-469c-4743-9ab8-10cd65676c9b}",
        "Lv2": "{df8ffcaf-e9ce-4632-a928-b930e3edd586}"
      },
      {
        "Lv1": "{2c0c0cd6-fb47-4dcd-973b-3af07eee7e7b}",
        "Lv2": "{d5518b4e-5f27-487d-bda9-36115e101cc9}"
      },
      {
        "Lv1": "{2c0c0cd6-fb47-4dcd-973b-3af07eee7e7b}",
        "Lv2": "{8f30601d-d3e7-42fc-a0ce-034d75124c33}"
      },
]
}

The idea here is draw branches from root to it's Lv1 and from Lv1 to Lv2. branches are connect between uuid of these level (root, Lv1, Lv2).

I have tried xpath to get the root and the first folder uuid, but have no way to get uuid from Lv2. Thanks.


Solution

  • I'm not familiar with dart, but to get the uuid's of elements that have a parent element with a uuid, you can use this XPath:

    //*[@uuid][parent::*[@uuid]]/@uuid
    

    it will get every value of a uuid-attribute of a element (because of //) that has a attribute @uuid and has a parent with also a @uuid attribute.

    To get the value of the uuid of the parent using XPath, use can use:

    //*[@uuid='value-uuid-previous-xpath']/parent::*/@uuid