Skip to main content Skip to complementary content

Mapping elements names and their values

Create a map to extract XML element names and values.

About this task

In this example, you want to map the previously created XML structure to get a JSON output containing the XML element names and their values in the key and value elements, respectively.

Procedure

  1. Create a JSON file with the following content and save it on your machine:
    {
       "elements":[
          {
             "key":"",
             "value":[
                {
                   "key":"",
                   "value":""
                }
             ]
          }
       ]
    }
  2. In Talend Data Mapper, create a structure based on your JSON file to use as output in your map.
  3. Create a standard map and add the XML structure as input and the JSON structure as output.
    For more information, see Creating a standard map.
  4. Drag and drop a GetElementProperty function on the output elements/key element.
  5. Drag and drop the input level1 element on the Element argument.
    This indicates that the value of the key element should be the name of the level1 element.
  6. Drag and drop a GetElementProperty function on the output elements/value/key element.
  7. Drag and drop the input level2 element on the Element argument.
  8. Drag and drop the input level2 element on the output elements/value/value element.
    Your map is configured, you can now test it.
  9. Import the XML samples provided in Mapping an XML structure with dynamic elements as sample input documents.
    For more information, see Importing a sample document.
  10. Click Test Run to see the result.

Results

With the first XML sample provided, the JSON output looks like this:
{
    "elements": [
        {
            "key": "book",
            "value": [
                {
                    "key": "title",
                    "value": "Pride and Prejudice"
                },
                {
                    "key": "author",
                    "value": "Jane Austen"
                },
                {
                    "key": "isbn",
                    "value": "978-0141439518"
                }
            ]
        },
        {
            "key": "book",
            "value": [
                {
                    "key": "title",
                    "value": "Jane Eyre"
                },
                {
                    "key": "author",
                    "value": "Charlotte Bronte"
                },
                {
                    "key": "isbn",
                    "value": "978-0553211405"
                }
            ]
        }
    ]
}
You can switch to the other sample and click Test Run again to get the following result:
{
    "elements": [
        {
            "key": "dvd",
            "value": [
                {
                    "key": "title",
                    "value": "Selma"
                },
                {
                    "key": "director",
                    "value": "Ava DuVernay"
                },
                {
                    "key": "released",
                    "value": "2014"
                }
            ]
        },
        {
            "key": "dvd",
            "value": [
                {
                    "key": "title",
                    "value": "The Hurt Locker"
                },
                {
                    "key": "director",
                    "value": "Kathryn Bigelow"
                },
                {
                    "key": "released",
                    "value": "2008"
                }
            ]
        }
    ]
}

Did this page help you?

If you find any issues with this page or its content – a typo, a missing step, or a technical error – please let us know!