Hi guys,
After making a layout, it just seemed so hard to do something that was quite simple conceptually.
I'm not very good at editing XML text by hand, and I thought I'd play around with firefox a little.
So if I launch firefox and go to about:blank, I've got an empty page.
Then if I hit F12, I get the Developer Tools window.
Firefox won't let you open local files with XMLHttpRequest because of CORS blocking, so let's make a textarea and paste our xml into it:
in the console:
ta = document.createElement("textarea")
document.body.append(ta)
and you should have a textarea in your blank page now, it's a little small so go resize it.
![[Linked Image from i.imgur.com]](https://i.imgur.com/KrBiotw.png)
Now locate a layout file in a text editor, select all the text and then paste it into the textarea.
so let's parse that xml:
par = new DOMParser()
>>> DOMParser { }
ser = new XMLSerializer
>>> XMLSerializer { }
xml = par.parseFromString(ta.value,"text/xml")
>>> XMLDocument { … }
and now you have magically parsed the xml into a DOM tree.
So you can access the elements with:
xml.querySelectorAll("element")
>>> NodeList(32) [ element, element, element, element, element, element, element, element, element, element, … ]
that returns a list of 32 elements, let's just focus on element 0:
xml.querySelectorAll("element")[0]
>>> <element name="devicetag_str" defstate="0">
so let's modify the name attribute:
xml.querySelectorAll("element")[0].setAttribute("name","was_devicetag_str")
ser.serializeToString(xml)
>>> <element name=\"was_devicetag_str\" defstate=\"0\">
so you can see that we have indeed modified the attribute name of the element.
and if you want to output the xml to your blank page, let's make another textarea and set the value to our serialized xml:
ta2 = document.createElement("textarea")
document.body.append(ta2)
ta2.value = ser.serializeToString(xml)
![[Linked Image from i.imgur.com]](https://i.imgur.com/64Wi4dK.png)
So let's try to make a new element and add it to the xml:
newelem = xml.createElement("element")
newelem.setAttribute("name","my_new_element")
newtext = xml.createElement("text")
newtext.setAttribute("string","a new string")
newelem.append(newtext)
xml.querySelector("mamelayout").prepend(newelem)
xml.querySelectorAll("element")[0]
>>> <element name="my_new_element">
ser.serializeToString(xml.querySelectorAll("element")[0])
>>> "<element name=\"my_new_element\"><text string=\"a new string\"/></element>"
![[Linked Image from i.imgur.com]](https://i.imgur.com/fQtrfQx.png)
So it's pretty easy to manipulate XML with javascript.
Next step is to try to make some resizable divs that would represent the various layout items like text and rect.
Another thing that's kinda cool to do is to open the xml file into firefox, then use the F12 developer tools to inspect the xml tree.