# XML Mode - get all attributes

**URL:** <https://discuss.codemirror.net/t/xml-mode-get-all-attributes/646>\
**Category:** Uncategorized\
**Created:** [February 10, 2016, 8:50am UTC](https://discuss.codemirror.net/t/xml-mode-get-all-attributes/646 "2016-02-10T08:50:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![matzebrock](https://discuss.codemirror.net/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@matzebrock](https://discuss.codemirror.net/u/matzebrock)\
**Post date:** [February 10, 2016, 8:50am UTC](https://discuss.codemirror.net/t/xml-mode-get-all-attributes/646/1 "2016-02-10T08:50:12Z")

</div>

Hi there,

is there a way to get all xml attributes with their values at the current cursor position?

The statement `<mytag id="1" name="Test"/>` should be parsed into something like:

> {  
> id: “1”,  
> name: “Test”  
> }

Thanks for any help or suggestions!

---

<div class="post-metadata">

**Author:** ![marijn](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/marijn/32/136_2.png) [@marijn](https://discuss.codemirror.net/u/marijn)\
**Post date:** [February 10, 2016, 9:26am UTC](https://discuss.codemirror.net/t/xml-mode-get-all-attributes/646/2 "2016-02-10T09:26:03Z")

</div>

Nope. You’ll have to do some parsing yourself.

---

<div class="post-metadata">

**Author:** ![matzebrock](https://discuss.codemirror.net/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@matzebrock](https://discuss.codemirror.net/u/matzebrock)\
**Post date:** [February 10, 2016, 3:43pm UTC](https://discuss.codemirror.net/t/xml-mode-get-all-attributes/646/3 "2016-02-10T15:43:01Z")

</div>

Thanks for your reply: here is my approach - maybe it helps someone:

```
cm.on("cursorActivity", function () {
	var cur = cm.getCursor(),
		tagList = [
			"control",
			"fql",
			"text"
		],
		range = cm.getViewport(),
		attributes = {};

	range.from = Math.min(range.from, cur.line);
	range.to = Math.max(cur.line + 1, range.to);

	var match = CodeMirror.findMatchingTag(cm, cur, range);

	if (match && match.at === "open" && tagList.indexOf(match.open.tag.toLowerCase()) > -1) {
		var doc = cm.getDoc(),
			tagLine = doc.getRange(match.open.from, match.open.to, false).join(""),
			re = /(\S+)\s*?=\s*(['"])(.*?|)\2/g,
			m;

		while ((m = re.exec(tagLine)) !== null) {
			if (m.index === re.lastIndex) {
				re.lastIndex++;
			}

			attributes[m[1]] = m[3];
		}

		console.log(attributes);
	}
});
```
