{"__v":0,"_id":"55fc9b7b3c21291900e7e0eb","category":{"__v":1,"_id":"55fc9b7a3c21291900e7e0de","pages":["55fc9b7b3c21291900e7e0e8","55fc9b7b3c21291900e7e0e9","55fc9b7b3c21291900e7e0ea","55fc9b7b3c21291900e7e0eb","55fc9b7b3c21291900e7e0ec"],"project":"5553d36165e8260d0024419a","version":"55fc9b793c21291900e7e0d7","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-05-14T20:15:44.034Z","from_sync":false,"order":6,"slug":"conditions","title":"Conditions"},"parentDoc":null,"project":"5553d36165e8260d0024419a","user":"5553d33065e8260d00244199","version":{"__v":12,"_id":"55fc9b793c21291900e7e0d7","project":"5553d36165e8260d0024419a","createdAt":"2015-09-18T23:17:13.545Z","releaseDate":"2015-09-18T23:17:13.545Z","categories":["55fc9b7a3c21291900e7e0d8","55fc9b7a3c21291900e7e0d9","55fc9b7a3c21291900e7e0da","55fc9b7a3c21291900e7e0db","55fc9b7a3c21291900e7e0dc","55fc9b7a3c21291900e7e0dd","55fc9b7a3c21291900e7e0de","55fc9b7a3c21291900e7e0df","55fc9b7a3c21291900e7e0e0","55fc9b7a3c21291900e7e0e1","55fc9b7a3c21291900e7e0e2","55fc9b7a3c21291900e7e0e3","55fc9bad4c80910d00b9bece","56394a0c3539050d00082122","56394a2f68b11f0d0048c9ff","56394a462fc5520d001a4db3","56394a4c2fc5520d001a4db4","56394a5b049e420d00476b5c","56394a6a2fc5520d001a4db5","56394f791593e4190073a44b","567b3adfb56bac0d0019d8d9","569d590e5a2b060d008741eb","569ea93dffccd10d00a05c7f"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"2.0.0","version":"2"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2015-09-01T00:00:12.208Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":3,"body":"We can handle parsing XML content in a similar way to the [JSON parsing](https://apimetrics.readme.io/docs/parsing-returned-content). The path we use is basic XPath syntax:\n[block:parameters]\n{\n  \"data\": {\n    \"0-0\": \"tag\",\n    \"0-1\": \"Selects all child elements with the given tag. For example, spam selects all child elements named spam, and spam/egg selects all grandchildren named egg in all children named spam.\",\n    \"1-0\": \"*\",\n    \"1-1\": \"Selects all child elements. For example, */egg selects all grandchildren named egg.\",\n    \"2-1\": \"Selects the current node. This is mostly useful at the beginning of the path, to indicate that it’s a relative path.\",\n    \"2-0\": \".\",\n    \"3-0\": \"//\",\n    \"4-0\": \"..\",\n    \"5-0\": \"[:::at:::attrib]\",\n    \"6-0\": \"[@attrib='value']\",\n    \"7-0\": \"[tag]\",\n    \"8-0\": \"[tag='text']\",\n    \"9-0\": \"[position]\",\n    \"3-1\": \"Selects all subelements, on all levels beneath the current element. For example, .//egg selects all egg elements in the entire tree.\",\n    \"4-1\": \"Selects the parent element.\",\n    \"5-1\": \"Selects all elements that have the given attribute.\",\n    \"6-1\": \"Selects all elements for which the given attribute has the given value. The value cannot contain quotes.\",\n    \"7-1\": \"Selects all elements that have a child named tag. Only immediate children are supported.\",\n    \"8-1\": \"Selects all elements that have a child named tag whose complete text content, including descendants, equals the given text.\",\n    \"9-1\": \"Selects all elements that are located at the given position. The position can be either an integer (1 is the first position), the expression last() (for the last position), or a position relative to the last position (e.g. last()-1).\"\n  },\n  \"cols\": 2,\n  \"rows\": 10\n}\n[/block]\nNote that the selector returns the *first* matching element. It then treats the result as a JSON structure, and anything after the **|** is treated by the [JSON parser](https://apimetrics.readme.io/docs/parsing-returned-content):\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"{\\n    \\\"tag\\\": \\\"<< element tag >>\\\",\\n    \\\"text\\\": \\\"<< element textual content >>\\\",\\n    \\\"attrib\\\": [\\\"<< element attributes in a list >>\\\"]\\n}\",\n      \"language\": \"json\"\n    }\n  ]\n}\n[/block]\nSo, if you want the attribute list from the first child XML node called slide, you could enter:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"slide|attrib\",\n      \"language\": \"text\"\n    }\n  ]\n}\n[/block]\nYou can then use any of the other filters from the JSON handling, as defined on our support site. So, with this XML:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<?xml version=\\\"1.0\\\" ?>\\n<!--  A SAMPLE set of slides  -->\\n<slideshow author=\\\"Yours Truly\\\" date=\\\"Date of publication\\\" title=\\\"Sample Slide Show\\\">\\n    <!-- TITLE SLIDE -->\\n    <slide type=\\\"all\\\">\\n        <title>Wake up to WonderWidgets!</title>\\n    </slide>\\n</slideshow>\\n\",\n      \"language\": \"xml\"\n    }\n  ]\n}\n[/block]\nYou could use:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"slide|attrib.type|upper\",\n      \"language\": \"text\"\n    }\n  ]\n}\n[/block]\n...will give you a variable set to \"ALL\"\nOr, \n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \" [@date]|tag|lower \",\n      \"language\": \"text\"\n    }\n  ]\n}\n[/block]\nwill give you a variable set to \"slideshow\".\n\nNote that these examples are all for the root element and it's direct children. To search anywhere in the tree, you need to specify:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \".//tagname\",\n      \"language\": \"text\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Namespaces\"\n}\n[/block]\nIf your XML response contains a namespace, the syntax is a bit complicated.\n\nTo search for a node anywhere in the XML tree, the syntax would be:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \".//{url-for-namespace}tagname\",\n      \"language\": \"text\"\n    }\n  ]\n}\n[/block]\nSo, for example, with a SOAP response you may need to use:\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \".//{http://schemas.xmlsoap.org/soap/envelope/}Body\",\n      \"language\": \"text\"\n    }\n  ]\n}\n[/block]","excerpt":"","slug":"parsing-xml-content","type":"basic","title":"Parsing XML Content"}

Parsing XML Content


We can handle parsing XML content in a similar way to the [JSON parsing](https://apimetrics.readme.io/docs/parsing-returned-content). The path we use is basic XPath syntax: [block:parameters] { "data": { "0-0": "tag", "0-1": "Selects all child elements with the given tag. For example, spam selects all child elements named spam, and spam/egg selects all grandchildren named egg in all children named spam.", "1-0": "*", "1-1": "Selects all child elements. For example, */egg selects all grandchildren named egg.", "2-1": "Selects the current node. This is mostly useful at the beginning of the path, to indicate that it’s a relative path.", "2-0": ".", "3-0": "//", "4-0": "..", "5-0": "[@attrib]", "6-0": "[@attrib='value']", "7-0": "[tag]", "8-0": "[tag='text']", "9-0": "[position]", "3-1": "Selects all subelements, on all levels beneath the current element. For example, .//egg selects all egg elements in the entire tree.", "4-1": "Selects the parent element.", "5-1": "Selects all elements that have the given attribute.", "6-1": "Selects all elements for which the given attribute has the given value. The value cannot contain quotes.", "7-1": "Selects all elements that have a child named tag. Only immediate children are supported.", "8-1": "Selects all elements that have a child named tag whose complete text content, including descendants, equals the given text.", "9-1": "Selects all elements that are located at the given position. The position can be either an integer (1 is the first position), the expression last() (for the last position), or a position relative to the last position (e.g. last()-1)." }, "cols": 2, "rows": 10 } [/block] Note that the selector returns the *first* matching element. It then treats the result as a JSON structure, and anything after the **|** is treated by the [JSON parser](https://apimetrics.readme.io/docs/parsing-returned-content): [block:code] { "codes": [ { "code": "{\n \"tag\": \"<< element tag >>\",\n \"text\": \"<< element textual content >>\",\n \"attrib\": [\"<< element attributes in a list >>\"]\n}", "language": "json" } ] } [/block] So, if you want the attribute list from the first child XML node called slide, you could enter: [block:code] { "codes": [ { "code": "slide|attrib", "language": "text" } ] } [/block] You can then use any of the other filters from the JSON handling, as defined on our support site. So, with this XML: [block:code] { "codes": [ { "code": "<?xml version=\"1.0\" ?>\n<!-- A SAMPLE set of slides -->\n<slideshow author=\"Yours Truly\" date=\"Date of publication\" title=\"Sample Slide Show\">\n <!-- TITLE SLIDE -->\n <slide type=\"all\">\n <title>Wake up to WonderWidgets!</title>\n </slide>\n</slideshow>\n", "language": "xml" } ] } [/block] You could use: [block:code] { "codes": [ { "code": "slide|attrib.type|upper", "language": "text" } ] } [/block] ...will give you a variable set to "ALL" Or, [block:code] { "codes": [ { "code": " [@date]|tag|lower ", "language": "text" } ] } [/block] will give you a variable set to "slideshow". Note that these examples are all for the root element and it's direct children. To search anywhere in the tree, you need to specify: [block:code] { "codes": [ { "code": ".//tagname", "language": "text" } ] } [/block] [block:api-header] { "type": "basic", "title": "Namespaces" } [/block] If your XML response contains a namespace, the syntax is a bit complicated. To search for a node anywhere in the XML tree, the syntax would be: [block:code] { "codes": [ { "code": ".//{url-for-namespace}tagname", "language": "text" } ] } [/block] So, for example, with a SOAP response you may need to use: [block:code] { "codes": [ { "code": ".//{http://schemas.xmlsoap.org/soap/envelope/}Body", "language": "text" } ] } [/block]