Parsing an XML file with Javascript

423 views
Skip to first unread message

Greg S.

unread,
Jun 8, 2011, 1:03:32 PM6/8/11
to CommandFusion
Hey,

I'm looking at using the new JS functionality to parse an XML file and
was wondering if there are any XML-specific functions that would make
the job easier. Any generic example allowing access to both Parent
and Child nodes/elements would be helpful.

Thanks for any help,

Greg

Jarrod Bell

unread,
Jun 8, 2011, 1:37:51 PM6/8/11
to comman...@googlegroups.com
I havent tried this, but you should be able to access any JavaScript
objects that are provided by Safari (as our JavaScript engine runs
inside a Safari view).

Here is a link that might get you started:
http://www.w3schools.com/Xml/xml_parser.asp

Then proceed to Google for more specific info you need.

Would love to hear if you get it working!

Jarrod

Florent Pillet

unread,
Jun 8, 2011, 3:07:52 PM6/8/11
to comman...@googlegroups.com
As explained in https://developer.mozilla.org/en/Parsing_and_serializing_XML
you can use the DOMParser object to parse XML. Remember that your JavaScript
is running inside a full-fledged (albeit invisible) HTML view and that you
can use a lot of the webview goodness.

Example:

var str='<tree><a id="a"><b id="b">hey!</b></a></tree>';
var parser = new DOMParser();
var xml = parser.parseFromString(str, "text/xml");
// xml.documentElement returns the "tree" element
for (var element in xml.documentElement.childNodes) {
// Go through your nodes here
}

You can find further reference on how to traverse the DOM
(Document Object Model) on the Mozilla website:

https://developer.mozilla.org/en/DOM

Florent

Greg S.

unread,
Jun 8, 2011, 3:34:36 PM6/8/11
to CommandFusion
Thanks Jarrod! I'll let you know how it goes.

Greg S.

unread,
Jun 9, 2011, 9:04:13 AM6/9/11
to CommandFusion
Guys,

Thanks for the links. I actually tried the method you both suggested
before asking, I just wasn't doing it correctly and thought maybe it
wasn't supported. Anyway, I did manage to get it going and it is
AWESOME! Parsing XML/RSS feeds with JS is so much easier and faster
than trying to do it with RegEx or Crestron's Simpl+ - once you figure
it out anyway! Once I get the project finished(or more refined) I'll
post some examples for others to learn from.

Thanks again,

Greg

Jarrod Bell

unread,
Jun 9, 2011, 9:05:56 AM6/9/11
to comman...@googlegroups.com
Excellent! Would be great if you could get used to using github for code
repository, as that is where we will be hosting a heap of example GUIs.

Jarrod

Greg S.

unread,
Jun 9, 2011, 10:53:37 AM6/9/11
to CommandFusion
Will look into it. Sounds like the Javascript is going to be very
successful and I definitely want to keep up with it. I've learned a
ton already from the LG TV project that was posted so I would love to
give back to the community in any way I can.

jbanik

unread,
Nov 30, 2011, 2:01:06 PM11/30/11
to comman...@googlegroups.com
Hey,

when do you think you can post your results? I'm testing here with my dreambox and it will not work....

jbanik

Greg S.

unread,
Nov 30, 2011, 3:23:41 PM11/30/11
to CommandFusion
It works. The CrestronMobile module uses it to talk with Crestron -
it's located in the GitHub repository. It was my first attempt at it
so there might be better, more efficient ways to do it, but it does
work.

Jarrod Bell

unread,
Nov 30, 2011, 9:43:01 PM11/30/11
to comman...@googlegroups.com
There is an example here on how to parse XML with JavaScript in our
software:

https://github.com/CommandFusion/DemoUserInterfaces

Check the "DynamicRoomList" example.

Jarrod

> --
> You received this message because you are subscribed to the Google
> Groups "CommandFusion" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/commandfusion/-/4FW8rRtjX-gJ.
> To post to this group, send email to comman...@googlegroups.com.
> To unsubscribe from this group, send email to
> commandfusio...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/commandfusion?hl=en.

Alexander S Argollo

unread,
Mar 30, 2013, 10:16:09 AM3/30/13
to comman...@googlegroups.com
HI Jorrod, i tried to running XML command on CF to control a Zigbee Gateway (this GW have a XML API), i reading on web that i need to including on CF a Java script to parsing a XML commads. 
I have used wireshark application to verify on network (TCP DATA)  how exacly the Gateway Web interface send to the Zigbee Gateway device, you can see it bellow:

Please, let me know, how can i implement or fix it?
are you have a tutorial how can i include it on Iviewer (guidesigner)?
each script i need to prepare to parsing XML command?
the XML code need to running on the target device (Zigbee GW)?
Are you have a examples to share with me?

Thanks a lot.

COMMAND ON (Light ON):
 .<zbpPacket>
  <Object>ZBP_Node</Object>
  <methodName>Node_ClusterCommand</methodName>
  <Arguments>
    <Argument type="uchar" base="10">2</Argument>
    <Argument type="ushort" base="10">9954</Argument>
    <Argument type="uchar" base="10">1</Argument>
    <Argument type="ushort" base="10">6</Argument>
    <Argument type="uchar" base="10">0</Argument>
    <Argument type="QByteArray" base="10">[]</Argument>
  </Arguments>
  <id>20989</id>
</zbpPacket>.<zbpPacket>
  <Object>ZBP_Node</Object>
  <methodName>Node_ClusterCommand</methodName>
  <Arguments>
    <Argument type="uchar" base="10">130</Argument>
    <Argument type="ushort" base="10">9954</Argument>
    <Argument type="uchar" base="10">1</Argument>
    <Argument type="ushort" base="10">6</Argument>
    <Argument type="uchar" base="10">0</Argument>
    <Argument type="QByteArray" base="10">[1,0,0]</Argument>
  </Arguments>
  <id>39075</id>
</zbpPacket>.

Jarrod Bell

unread,
Mar 30, 2013, 7:21:17 PM3/30/13
to comman...@googlegroups.com
What communication method does the gateway use? All you have mentioned so far is that it uses XML data, but no mention of how it is sent/received.

If you want to parse a reply from the gateway, then you should use the JavaScript XML features (standard JavaScript methods).
You can find an example of how that is done in the Dynamic Room List example on our Demo User Interfaces Github repo:
https://github.com/CommandFusion/DemoUserInterfaces
https://github.com/CommandFusion/DemoUserInterfaces/tree/master/DynamicRoomList
https://github.com/CommandFusion/DemoUserInterfaces/blob/master/DynamicRoomList/roomlist.js#L96

Regards,

Jarrod Bell
CommandFusion
www.commandfusion.com


--
You received this message because you are subscribed to the Google Groups "CommandFusion Software" group.
To unsubscribe from this group and stop receiving emails from it, send an email to commandfusio...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages