cancel
Showing results for 
Search instead for 
Did you mean: 

Parsing an XML Document in get.js file

lilyh
Champ in-the-making
Champ in-the-making
Hello,
I have written a web script to load part of a HTML file. Within the web script I need to extract the content from an XML file.
I have attempted to parseXMLDocument within get.html.ftl but came across a similar problem to that of:

http://forums.alfresco.com/en/viewtopic.php?f=36&t=21315

Instead of attempting to parse the XML document in the freemarker file, I am now trying to load the XML file in the javascript get.js  file.
I have found a lot of resources on loading XML documents in javascript using a file name but I am now passing in a node and have been unable to find references for this.

Is the latter approach correct (loading XML in the get.js file)? If so, are there any resources on how this is done. If not, any pointers on how it should/could be done will be greatly appreciated.
Thank you!

Lily.
1 ACCEPTED ANSWER

pmonks
Star Contributor
Star Contributor
This is possible by reading the content property of the node then using E4X [1],[2],[3] to parse that content.  The following code (note: written from memory - almost certainly contains errors) shows one way of doing this:


var node = search.findNode(nodeRef)
var content = node.content
var xml = new XML(content)
var valueOfSomeOtherElement = xml.rootElement.childElement.someOtherElement
Line 1 uses the Alfresco "search" API to find the node, given the string value of the NodeRef (eg. "workspace:/SpacesStore/d8470f03-b80a-11da-97b9-8d453af5a554").
Line 2 reads the content of that node (note: you should check that the node was successfully loaded before attempting to do this - for clarity I omitted that logic in this example).
Lines 3 and 4 uses E4X constructs to parse the XML content and retrieve the value of some element within the XML (note: you should check that the MIME type of the loaded node is "text/xml" before attempting to parse the XML - for clarity I omitted that logic in this example).

Cheers,
Peter

[1] http://en.wikipedia.org/wiki/ECMAScript_for_XML
[2] http://www.xml.com/pub/a/2007/11/28/introducing-e4x.html
[3] http://www.w3schools.com/e4x/default.asp

View answer in original post

3 REPLIES 3

pmonks
Star Contributor
Star Contributor
This is possible by reading the content property of the node then using E4X [1],[2],[3] to parse that content.  The following code (note: written from memory - almost certainly contains errors) shows one way of doing this:


var node = search.findNode(nodeRef)
var content = node.content
var xml = new XML(content)
var valueOfSomeOtherElement = xml.rootElement.childElement.someOtherElement
Line 1 uses the Alfresco "search" API to find the node, given the string value of the NodeRef (eg. "workspace:/SpacesStore/d8470f03-b80a-11da-97b9-8d453af5a554").
Line 2 reads the content of that node (note: you should check that the node was successfully loaded before attempting to do this - for clarity I omitted that logic in this example).
Lines 3 and 4 uses E4X constructs to parse the XML content and retrieve the value of some element within the XML (note: you should check that the MIME type of the loaded node is "text/xml" before attempting to parse the XML - for clarity I omitted that logic in this example).

Cheers,
Peter

[1] http://en.wikipedia.org/wiki/ECMAScript_for_XML
[2] http://www.xml.com/pub/a/2007/11/28/introducing-e4x.html
[3] http://www.w3schools.com/e4x/default.asp

thanks is working.

lilyh
Champ in-the-making
Champ in-the-making
Thank you Peter.
That was really helpful.