Anyone know of a javascript function that will take an xml document and
turn it into an associative array?
Basically it would take something like this:
<body>
<item>
<id>1</id>
<name>Bob</name>
</item>
<item>
<id>2</id>
<name>John</name>
</item>
</body>
And turn it into something like this:
{
itemList: [
{
id: 1,
name: ''Bob''
},
{
id: 2,
name: ''John''
}
]
}
If not, I''m gonna build one.
Greg
_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
> If not, I''m gonna build one.Cool, make sure it can handle both elements and attributes. For instance, how would your example be different if my XML doc was... <body> <item id="1" name="Bob" /> <item id="2" name="John" /> </body> The information transmitted in this electronic mail is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers. _______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
Good point, I hadn''t thought of that ''cause the project I was needing it for didn''t use them. I''ll have to think about that for a bit. Greg ________________________________ From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Ryan Gahl Sent: Friday, March 17, 2006 8:48 AM To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: RE: [Rails-spinoffs] good javascript xml parser> If not, I''m gonna build one.Cool, make sure it can handle both elements and attributes. For instance, how would your example be different if my XML doc was... <body> <item id="1" name="Bob" /> <item id="2" name="John" /> </body> The information transmitted in this electronic mail is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers. _______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
If you are only using for server-client communications, you might
consider JSON. I needed something very basic as well, but just decided
to go with JSON instead of XML since it has less markup and is just
plain simpler. Using XML with prototype was more trouble than it was
worth for me. Usage is simple as can be, just call json_encode or
json_decode (I use PHP but I''m sure similar functions are available for
whatever you use).
JS version of json_decode:
function json_decode(txt){
var r = null;
eval(''r=''+txt+'';'');
return r;
}
If you must use XML, I don''t know of anything very lightweight and
simple but I''d love to see something like what you''re talking
about.
Thanks,
Colin
Gregory Hill wrote:
Anyone know of a
javascript function that will take an xml
document and turn it into an associative array?
Basically it would take something like this:
1
Bob
2
John
And turn it into something like this:
{
itemList: [
{
id: 1,
name: ‘Bob’
},
{
id: 2,
name: ‘John’
}
]
}
If not, I’m gonna build
one.
Greg
_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
Maybe this would help you :)
Thank you,
Mandy.
p.s. I have not written this. I got this from some site long time back.
XMLParser = Class.create();
Object.extend(XMLParser.prototype, {
initialize: function(xmlObj) {
this.xmlObj = xmlObj;
this.root = xmlObj.documentElement;
},
list: function(value) {
var result = this.root.getElementsByTagName(value);
return result;
},
get: function (node, value) {
if(typeof value != ''undefined'')
node = node.getElementsByTagName(value).item(0);
return this.text(node);
},
toArray: function(value) {
var list = this.list(value);
var nodeValues = new Array();
for (var i = 0; i < list.length; i++) {
nodeValues.push(this.get(list[i]));
}
return nodeValues;
},
text: function (node) {
if (typeof node.textContent != ''undefined'') {
return node.textContent;
}
else if (typeof node.innerText != ''undefined'') {
return node.innerText;
}
else if (typeof node.text != ''undefined'') {
return node.text;
}
else {
switch (node.nodeType) {
case 3:
case 4:
return node.nodeValue;
break;
case 1:
case 11:
var innerText = '''';
for (var i = 0; i < node.childNodes.length; i++)
{
innerText += text(node.childNodes[i]);
}
return innerText;
break;
default:
return '''';
}
}
}
});
XMLParser.nodeType = [
"",
"ELEMENT_NODE", // 1
"ATTRIBUTE_NODE", // 2
"TEXT_NODE", // 3
"CDATA_SECTION_NODE", // 4
"ENTITY_REFERENCE_NODE", // 5
"ENTITY_NODE", // 6
"PROCESSING_INSTRUCTION_NODE", // 7
"COMMENT_NODE", // 8
"DOCUMENT_NODE", // 9
"DOCUMENT_TYPE_NODE", // 10
"DOCUMENT_FRAGMENT_NODE", // 11
"NOTATION_NODE" // 12
];
<*cript language="javascript" type="text/javascript">
var xmlParser = new XMLParser(originalRequest.responseXML);
var items = xmlParser.list(''item'');
for (var i = 0; i < items.length; i++) {
var id = xmlParser.get(items[i], ''id'');
var name = xmlParser.get(items[i], ''name'');
$(''result'').value += id + '', '' + name +
''\r\n'';
}
var names = xmlParser.list(''name'');
for (var i = 0; i < names.length; i++) {
$(''result'').value += xmlParser.get(names[i]) +
''\r\n'';
}
var ids = xmlParser.toArray(''id'');
for (var i = 0; i < ids.length; i++) {
$(''result'').value += ''id:'' + ids[i] +
''\r\n'';
}
</*cript>
<textarea id="result" cols="60" rows="10"
></textarea>
<?xml version="1.0" encoding=''euc-kr''?>
<list>
<item>
<id>1</id>
<name>a1</name>
</item>
<item>
<id>2</id>
<name>a2</name>
</item>
<item>
<id>3</id>
<name>a3</name>
</item>
</list>
_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
Hmm... that is a pretty cool class. I was hoping more for something
that you didn''t need to know what the tag names were, it would just
create a hierarchy that you could drill down into easily, but if I get
stuck, this is a good backup.
Greg
________________________________
From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
[mailto:rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org]
On Behalf Of
Maninder, Singh
Sent: Friday, March 17, 2006 9:34 AM
To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
Subject: RE: [Rails-spinoffs] good javascript xml parser
Maybe this would help you :)
Thank you,
Mandy.
p.s. I have not written this. I got this from some site long time back.
XMLParser = Class.create();
Object.extend(XMLParser.prototype, {
initialize: function(xmlObj) {
this.xmlObj = xmlObj;
this.root = xmlObj.documentElement;
},
list: function(value) {
var result = this.root.getElementsByTagName(value);
return result;
},
get: function (node, value) {
if(typeof value != ''undefined'')
node = node.getElementsByTagName(value).item(0);
return this.text(node);
},
toArray: function(value) {
var list = this.list(value);
var nodeValues = new Array();
for (var i = 0; i < list.length; i++) {
nodeValues.push(this.get(list[i]));
}
return nodeValues;
},
text: function (node) {
if (typeof node.textContent != ''undefined'') {
return node.textContent;
}
else if (typeof node.innerText != ''undefined'') {
return node.innerText;
}
else if (typeof node.text != ''undefined'') {
return node.text;
}
else {
switch (node.nodeType) {
case 3:
case 4:
return node.nodeValue;
break;
case 1:
case 11:
var innerText = '''';
for (var i = 0; i < node.childNodes.length; i++)
{
innerText += text(node.childNodes[i]);
}
return innerText;
break;
default:
return '''';
}
}
}
});
XMLParser.nodeType = [
"",
"ELEMENT_NODE", // 1
"ATTRIBUTE_NODE", // 2
"TEXT_NODE", // 3
"CDATA_SECTION_NODE", // 4
"ENTITY_REFERENCE_NODE", // 5
"ENTITY_NODE", // 6
"PROCESSING_INSTRUCTION_NODE", // 7
"COMMENT_NODE", // 8
"DOCUMENT_NODE", // 9
"DOCUMENT_TYPE_NODE", // 10
"DOCUMENT_FRAGMENT_NODE", // 11
"NOTATION_NODE" // 12
];
<*cript language="javascript" type="text/javascript">
var xmlParser = new XMLParser(originalRequest.responseXML);
var items = xmlParser.list(''item'');
for (var i = 0; i < items.length; i++) {
var id = xmlParser.get(items[i], ''id'');
var name = xmlParser.get(items[i], ''name'');
$(''result'').value += id + '', '' + name +
''\r\n'';
}
var names = xmlParser.list(''name'');
for (var i = 0; i < names.length; i++) {
$(''result'').value += xmlParser.get(names[i]) +
''\r\n'';
}
var ids = xmlParser.toArray(''id'');
for (var i = 0; i < ids.length; i++) {
$(''result'').value += ''id:'' + ids[i] +
''\r\n'';
}
</*cript>
<textarea id="result" cols="60" rows="10"
></textarea>
<?xml version="1.0" encoding=''euc-kr''?>
<list>
<item>
<id>1</id>
<name>a1</name>
</item>
<item>
<id>2</id>
<name>a2</name>
</item>
<item>
<id>3</id>
<name>a3</name>
</item>
</list>
_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
This might not be practical for you, but the google maps api has an xml parser that uses a native xml parser if the browser has one and a javascript parser if the browser doesn''t. http://www.google.com/apis/maps/documentation/#GXml_code_ Also, just found http://xmljs.sourceforge.net/ Tom Riley <http://www.smallroomsoftware.com> On 17 Mar 2006, at 16:46, Gregory Hill wrote:> Hmm… that is a pretty cool class. I was hoping more for something > that you didn’t need to know what the tag names were, it would just > create a hierarchy that you could drill down into easily, but if I > get stuck, this is a good backup. > > > > Greg > > > > From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails- > spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Maninder, Singh > Sent: Friday, March 17, 2006 9:34 AM > To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > Subject: RE: [Rails-spinoffs] good javascript xml parser > > > > Maybe this would help you :) > > > > Thank you, > Mandy. > > > > p.s. I have not written this. I got this from some site long time > back. > > > > > > XMLParser = Class.create(); > Object.extend(XMLParser.prototype, { > initialize: function(xmlObj) { > this.xmlObj = xmlObj; > this.root = xmlObj.documentElement; > }, > list: function(value) { > var result = this.root.getElementsByTagName(value); > return result; > }, > get: function (node, value) { > if(typeof value != ''undefined'') > node = node.getElementsByTagName(value).item(0); > return this.text(node); > }, > toArray: function(value) { > var list = this.list(value); > var nodeValues = new Array(); > for (var i = 0; i < list.length; i++) { > nodeValues.push(this.get(list[i])); > } > return nodeValues; > }, > text: function (node) { > if (typeof node.textContent != ''undefined'') { > return node.textContent; > } > else if (typeof node.innerText != ''undefined'') { > return node.innerText; > } > else if (typeof node.text != ''undefined'') { > return node.text; > } > else { > switch (node.nodeType) { > case 3: > case 4: > return node.nodeValue; > break; > case 1: > case 11: > var innerText = ''''; > for (var i = 0; i < node.childNodes.length; i++) > { > innerText += text(node.childNodes[i]); > } > return innerText; > break; > default: > return ''''; > } > } > } > }); > XMLParser.nodeType = [ > "", > "ELEMENT_NODE", // 1 > "ATTRIBUTE_NODE", // 2 > "TEXT_NODE", // 3 > "CDATA_SECTION_NODE", // 4 > "ENTITY_REFERENCE_NODE", // 5 > "ENTITY_NODE", // 6 > "PROCESSING_INSTRUCTION_NODE", // 7 > "COMMENT_NODE", // 8 > "DOCUMENT_NODE", // 9 > "DOCUMENT_TYPE_NODE", // 10 > "DOCUMENT_FRAGMENT_NODE", // 11 > "NOTATION_NODE" // 12 > ]; > > > > <*cript language="javascript" type="text/javascript"> > var xmlParser = new XMLParser(originalRequest.responseXML); > var items = xmlParser.list(''item''); > for (var i = 0; i < items.length; i++) { > var id = xmlParser.get(items[i], ''id''); > var name = xmlParser.get(items[i], ''name''); > > $(''result'').value += id + '', '' + name + ''\r\n''; > } > var names = xmlParser.list(''name''); > for (var i = 0; i < names.length; i++) { > $(''result'').value += xmlParser.get(names[i]) + ''\r\n''; > } > var ids = xmlParser.toArray(''id''); > for (var i = 0; i < ids.length; i++) { > $(''result'').value += ''id:'' + ids[i] + ''\r\n''; > } > </*cript> > <textarea id="result" cols="60" rows="10" ></textarea> > > > > <?xml version="1.0" encoding=''euc-kr''?> > <list> > <item> > <id>1</id> > <name>a1</name> > </item> > <item> > <id>2</id> > <name>a2</name> > </item> > <item> > <id>3</id> > <name>a3</name> > </item> > </list> > > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs_______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
It''s pretty quick-and-dirty, but this is my first attempt.
function xmlToHash(el) {
Element.cleanWhitespace(el);
if (el.hasAttributes() || (el.hasChildNodes() &&
el.childNodes[0].nodeType == 1)) {
var localHash = {};
if (el.hasAttributes && el.attributes.length >= 1) {
$A(el.attributes).each(function (attr) { localHash[attr.nodeName]
= attr.nodeValue; });
}
$A(el.childNodes).each(function (el) { xmlToHashElement(localHash,
el); });
for (attr in localHash) {
if (localHash[attr].length == 1) {
localHash[attr] = localHash[attr][0];
}
}
return localHash;
}
else {
return el.textContent || '''';
}
}
function xmlToHashElement (hash, el) {
if (el.nodeType == 2) {
hash[el.localName] = [ el.textContent ];
}
else {
var key = el.tagName;
if (hash[key]) {
hash[key].push(xmlToHash(el));
}
else {
hash[key] = [ xmlToHash(el) ];
}
}
}
You just do:
var hash = xmlToHash(response.responseXML.documentElement);
The tag name of the child nodes become the keys in the hash, and if
there are more than one with the same tag name, it is an array as the
value. It handles attributes as well, with the attribute name becoming
the hash key and the value the value. I imagine if you had a child node
with a tag name that matched the attribute, it would overwrite the
attribute. I didn''t think that was worth handling, as I
couldn''t think
of a legitimate scenario where it would be desirable.
And it''s recursive, so you can put nodes within nodes and it will
create
hashes out of them. I imagine it doesn''t handle every possible
scenario, but it''s a decent start. You need to load prototype to use
this, btw.
Greg
________________________________
From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
[mailto:rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org]
On Behalf Of Tom
Riley
Sent: Friday, March 17, 2006 9:56 AM
To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
Subject: Re: [Rails-spinoffs] good javascript xml parser
This might not be practical for you, but the google maps api has an xml
parser that uses a native xml parser if the browser has one and a
javascript parser if the browser doesn''t.
http://www.google.com/apis/maps/documentation/#GXml_code_
Also, just found http://xmljs.sourceforge.net/
Tom Riley
<http://www.smallroomsoftware.com>
On 17 Mar 2006, at 16:46, Gregory Hill wrote:
Hmm... that is a pretty cool class. I was hoping more for something
that you didn''t need to know what the tag names were, it would just
create a hierarchy that you could drill down into easily, but if I get
stuck, this is a good backup.
Greg
________________________________
From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
[mailto:rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org]
On Behalf Of
Maninder, Singh
Sent: Friday, March 17, 2006 9:34 AM
To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
Subject: RE: [Rails-spinoffs] good javascript xml parser
Maybe this would help you :)
Thank you,
Mandy.
p.s. I have not written this. I got this from some site long time back.
XMLParser = Class.create();
Object.extend(XMLParser.prototype, {
initialize: function(xmlObj) {
this.xmlObj = xmlObj;
this.root = xmlObj.documentElement;
},
list: function(value) {
var result = this.root.getElementsByTagName(value);
return result;
},
get: function (node, value) {
if(typeof value != ''undefined'')
node = node.getElementsByTagName(value).item(0);
return this.text(node);
},
toArray: function(value) {
var list = this.list(value);
var nodeValues = new Array();
for (var i = 0; i < list.length; i++) {
nodeValues.push(this.get(list[i]));
}
return nodeValues;
},
text: function (node) {
if (typeof node.textContent != ''undefined'') {
return node.textContent;
}
else if (typeof node.innerText != ''undefined'') {
return node.innerText;
}
else if (typeof node.text != ''undefined'') {
return node.text;
}
else {
switch (node.nodeType) {
case 3:
case 4:
return node.nodeValue;
break;
case 1:
case 11:
var innerText = '''';
for (var i = 0; i < node.childNodes.length; i++)
{
innerText += text(node.childNodes[i]);
}
return innerText;
break;
default:
return '''';
}
}
}
});
XMLParser.nodeType = [
"",
"ELEMENT_NODE", // 1
"ATTRIBUTE_NODE", // 2
"TEXT_NODE", // 3
"CDATA_SECTION_NODE", // 4
"ENTITY_REFERENCE_NODE", // 5
"ENTITY_NODE", // 6
"PROCESSING_INSTRUCTION_NODE", // 7
"COMMENT_NODE", // 8
"DOCUMENT_NODE", // 9
"DOCUMENT_TYPE_NODE", // 10
"DOCUMENT_FRAGMENT_NODE", // 11
"NOTATION_NODE" // 12
];
<*cript language="javascript" type="text/javascript">
var xmlParser = new XMLParser(originalRequest.responseXML);
var items = xmlParser.list(''item'');
for (var i = 0; i < items.length; i++) {
var id = xmlParser.get(items[i], ''id'');
var name = xmlParser.get(items[i], ''name'');
$(''result'').value += id + '', '' + name +
''\r\n'';
}
var names = xmlParser.list(''name'');
for (var i = 0; i < names.length; i++) {
$(''result'').value += xmlParser.get(names[i]) +
''\r\n'';
}
var ids = xmlParser.toArray(''id'');
for (var i = 0; i < ids.length; i++) {
$(''result'').value += ''id:'' + ids[i] +
''\r\n'';
}
</*cript>
<textarea id="result" cols="60" rows="10"
></textarea>
<?xml version="1.0" encoding=''euc-kr''?>
<list>
<item>
<id>1</id>
<name>a1</name>
</item>
<item>
<id>2</id>
<name>a2</name>
</item>
<item>
<id>3</id>
<name>a3</name>
</item>
</list>
_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
Oops... it doesn''t work at all in IE. Friggin IE.
________________________________
From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
[mailto:rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org]
On Behalf Of
Gregory Hill
Sent: Friday, March 17, 2006 11:20 AM
To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
Subject: RE: [Rails-spinoffs] good javascript xml parser
It''s pretty quick-and-dirty, but this is my first attempt.
function xmlToHash(el) {
Element.cleanWhitespace(el);
if (el.hasAttributes() || (el.hasChildNodes() &&
el.childNodes[0].nodeType == 1)) {
var localHash = {};
if (el.hasAttributes && el.attributes.length >= 1) {
$A(el.attributes).each(function (attr) { localHash[attr.nodeName]
= attr.nodeValue; });
}
$A(el.childNodes).each(function (el) { xmlToHashElement(localHash,
el); });
for (attr in localHash) {
if (localHash[attr].length == 1) {
localHash[attr] = localHash[attr][0];
}
}
return localHash;
}
else {
return el.textContent || '''';
}
}
function xmlToHashElement (hash, el) {
if (el.nodeType == 2) {
hash[el.localName] = [ el.textContent ];
}
else {
var key = el.tagName;
if (hash[key]) {
hash[key].push(xmlToHash(el));
}
else {
hash[key] = [ xmlToHash(el) ];
}
}
}
You just do:
var hash = xmlToHash(response.responseXML.documentElement);
The tag name of the child nodes become the keys in the hash, and if
there are more than one with the same tag name, it is an array as the
value. It handles attributes as well, with the attribute name becoming
the hash key and the value the value. I imagine if you had a child node
with a tag name that matched the attribute, it would overwrite the
attribute. I didn''t think that was worth handling, as I
couldn''t think
of a legitimate scenario where it would be desirable.
And it''s recursive, so you can put nodes within nodes and it will
create
hashes out of them. I imagine it doesn''t handle every possible
scenario, but it''s a decent start. You need to load prototype to use
this, btw.
Greg
________________________________
From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
[mailto:rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org]
On Behalf Of Tom
Riley
Sent: Friday, March 17, 2006 9:56 AM
To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
Subject: Re: [Rails-spinoffs] good javascript xml parser
This might not be practical for you, but the google maps api has an xml
parser that uses a native xml parser if the browser has one and a
javascript parser if the browser doesn''t.
http://www.google.com/apis/maps/documentation/#GXml_code_
Also, just found http://xmljs.sourceforge.net/
Tom Riley
<http://www.smallroomsoftware.com>
On 17 Mar 2006, at 16:46, Gregory Hill wrote:
Hmm... that is a pretty cool class. I was hoping more for something
that you didn''t need to know what the tag names were, it would just
create a hierarchy that you could drill down into easily, but if I get
stuck, this is a good backup.
Greg
________________________________
From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
[mailto:rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org]
On Behalf Of
Maninder, Singh
Sent: Friday, March 17, 2006 9:34 AM
To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
Subject: RE: [Rails-spinoffs] good javascript xml parser
Maybe this would help you :)
Thank you,
Mandy.
p.s. I have not written this. I got this from some site long time back.
XMLParser = Class.create();
Object.extend(XMLParser.prototype, {
initialize: function(xmlObj) {
this.xmlObj = xmlObj;
this.root = xmlObj.documentElement;
},
list: function(value) {
var result = this.root.getElementsByTagName(value);
return result;
},
get: function (node, value) {
if(typeof value != ''undefined'')
node = node.getElementsByTagName(value).item(0);
return this.text(node);
},
toArray: function(value) {
var list = this.list(value);
var nodeValues = new Array();
for (var i = 0; i < list.length; i++) {
nodeValues.push(this.get(list[i]));
}
return nodeValues;
},
text: function (node) {
if (typeof node.textContent != ''undefined'') {
return node.textContent;
}
else if (typeof node.innerText != ''undefined'') {
return node.innerText;
}
else if (typeof node.text != ''undefined'') {
return node.text;
}
else {
switch (node.nodeType) {
case 3:
case 4:
return node.nodeValue;
break;
case 1:
case 11:
var innerText = '''';
for (var i = 0; i < node.childNodes.length; i++)
{
innerText += text(node.childNodes[i]);
}
return innerText;
break;
default:
return '''';
}
}
}
});
XMLParser.nodeType = [
"",
"ELEMENT_NODE", // 1
"ATTRIBUTE_NODE", // 2
"TEXT_NODE", // 3
"CDATA_SECTION_NODE", // 4
"ENTITY_REFERENCE_NODE", // 5
"ENTITY_NODE", // 6
"PROCESSING_INSTRUCTION_NODE", // 7
"COMMENT_NODE", // 8
"DOCUMENT_NODE", // 9
"DOCUMENT_TYPE_NODE", // 10
"DOCUMENT_FRAGMENT_NODE", // 11
"NOTATION_NODE" // 12
];
<*cript language="javascript" type="text/javascript">
var xmlParser = new XMLParser(originalRequest.responseXML);
var items = xmlParser.list(''item'');
for (var i = 0; i < items.length; i++) {
var id = xmlParser.get(items[i], ''id'');
var name = xmlParser.get(items[i], ''name'');
$(''result'').value += id + '', '' + name +
''\r\n'';
}
var names = xmlParser.list(''name'');
for (var i = 0; i < names.length; i++) {
$(''result'').value += xmlParser.get(names[i]) +
''\r\n'';
}
var ids = xmlParser.toArray(''id'');
for (var i = 0; i < ids.length; i++) {
$(''result'').value += ''id:'' + ids[i] +
''\r\n'';
}
</*cript>
<textarea id="result" cols="60" rows="10"
></textarea>
<?xml version="1.0" encoding=''euc-kr''?>
<list>
<item>
<id>1</id>
<name>a1</name>
</item>
<item>
<id>2</id>
<name>a2</name>
</item>
<item>
<id>3</id>
<name>a3</name>
</item>
</list>
_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
A bit OT, but have a look at E4X. It''s only supported in mozilla, but you might be able to write an emulation for IE On 3/17/06, Gregory Hill <Gregory_Hill-l9nu40+TWxQ@public.gmane.org> wrote:> > > > It''s pretty quick-and-dirty, but this is my first attempt. > > > > function xmlToHash(el) { > > Element.cleanWhitespace(el); > > if (el.hasAttributes() || (el.hasChildNodes() && el.childNodes[0].nodeType > == 1)) { > > var localHash = {}; > > if (el.hasAttributes && el.attributes.length >= 1) { > > $A(el.attributes).each(function (attr) { localHash[attr.nodeName] > attr.nodeValue; }); > > } > > $A(el.childNodes).each(function (el) { xmlToHashElement(localHash, el); > }); > > for (attr in localHash) { > > if (localHash[attr].length == 1) { > > localHash[attr] = localHash[attr][0]; > > } > > } > > return localHash; > > } > > else { > > return el.textContent || ''''; > > } > > } > > > > function xmlToHashElement (hash, el) { > > if (el.nodeType == 2) { > > hash[el.localName] = [ el.textContent ]; > > } > > else { > > var key = el.tagName; > > if (hash[key]) { > > hash[key].push(xmlToHash(el)); > > } > > else { > > hash[key] = [ xmlToHash(el) ]; > > } > > } > > } > > > > You just do: > > > > var hash = xmlToHash(response.responseXML.documentElement); > > > > The tag name of the child nodes become the keys in the hash, and if there > are more than one with the same tag name, it is an array as the value. It > handles attributes as well, with the attribute name becoming the hash key > and the value the value. I imagine if you had a child node with a tag name > that matched the attribute, it would overwrite the attribute. I didn''t > think that was worth handling, as I couldn''t think of a legitimate scenario > where it would be desirable. > > > > And it''s recursive, so you can put nodes within nodes and it will create > hashes out of them. I imagine it doesn''t handle every possible scenario, > but it''s a decent start. You need to load prototype to use this, btw. > > > > Greg > > > > > > > > ________________________________ > > > From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > [mailto:rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On > Behalf Of Tom Riley > Sent: Friday, March 17, 2006 9:56 AM > > To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > Subject: Re: [Rails-spinoffs] good javascript xml parser > > > To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > Subject: Re: [Rails-spinoffs] good javascript xml parser > > > > > > > This might not be practical for you, but the google maps api has an xml > parser that uses a native xml parser if the browser has one and a javascript > parser if the browser doesn''t. > > > > > > http://www.google.com/apis/maps/documentation/#GXml_code_ > > > > > > Also, just found http://xmljs.sourceforge.net/ > > > > > Tom Riley > > > > > > <http://www.smallroomsoftware.com> > > > > > > > > > On 17 Mar 2006, at 16:46, Gregory Hill wrote: > > > > > > Hmm… that is a pretty cool class. I was hoping more for something that you > didn''t need to know what the tag names were, it would just create a > hierarchy that you could drill down into easily, but if I get stuck, this is > a good backup. > > > > Greg > > > > ________________________________ > > > From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > [mailto:rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On > Behalf Of Maninder, Singh > Sent: Friday, March 17, 2006 9:34 AM > To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > Subject: RE: [Rails-spinoffs] good javascript xml parser > > > > > Maybe this would help you :) > > > > > > Thank you, > Mandy. > > > > > > p.s. I have not written this. I got this from some site long time back. > > > > > > > > > XMLParser = Class.create(); > Object.extend(XMLParser.prototype, { > initialize: function(xmlObj) { > this.xmlObj = xmlObj; > this.root = xmlObj.documentElement; > }, > list: function(value) { > var result = this.root.getElementsByTagName(value); > return result; > }, > get: function (node, value) { > if(typeof value != ''undefined'') > node = node.getElementsByTagName(value).item(0); > return this.text(node); > }, > toArray: function(value) { > var list = this.list(value); > var nodeValues = new Array(); > for (var i = 0; i < list.length; i++) { > nodeValues.push(this.get(list[i])); > } > return nodeValues; > }, > text: function (node) { > if (typeof node.textContent != ''undefined'') { > return node.textContent; > } > else if (typeof node.innerText != ''undefined'') { > return node.innerText; > } > else if (typeof node.text != ''undefined'') { > return node.text; > } > else { > switch (node.nodeType) { > case 3: > case 4: > return node.nodeValue; > break; > case 1: > case 11: > var innerText = ''''; > for (var i = 0; i < node.childNodes.length; i++) > { > innerText += text(node.childNodes[i]); > } > return innerText; > break; > default: > return ''''; > } > } > } > }); > XMLParser.nodeType = [ > "", > "ELEMENT_NODE", // 1 > "ATTRIBUTE_NODE", // 2 > "TEXT_NODE", // 3 > "CDATA_SECTION_NODE", // 4 > "ENTITY_REFERENCE_NODE", // 5 > "ENTITY_NODE", // 6 > "PROCESSING_INSTRUCTION_NODE", // 7 > "COMMENT_NODE", // 8 > "DOCUMENT_NODE", // 9 > "DOCUMENT_TYPE_NODE", // 10 > "DOCUMENT_FRAGMENT_NODE", // 11 > "NOTATION_NODE" // 12 > ]; > > > > > > <*cript language="javascript" type="text/javascript"> > var xmlParser = new XMLParser(originalRequest.responseXML); > var items = xmlParser.list(''item''); > for (var i = 0; i < items.length; i++) { > var id = xmlParser.get(items[i], ''id''); > var name = xmlParser.get(items[i], ''name''); > > $(''result'').value += id + '', '' + name + ''\r\n''; > } > var names = xmlParser.list(''name''); > for (var i = 0; i < names.length; i++) { > $(''result'').value += xmlParser.get(names[i]) + ''\r\n''; > } > var ids = xmlParser.toArray(''id''); > for (var i = 0; i < ids.length; i++) { > $(''result'').value += ''id:'' + ids[i] + ''\r\n''; > } > </*cript> > <textarea id="result" cols="60" rows="10" ></textarea> > > > > > > <?xml version="1.0" encoding=''euc-kr''?> > <list> > <item> > <id>1</id> > <name>a1</name> > </item> > <item> > <id>2</id> > <name>a2</name> > </item> > <item> > <id>3</id> > <name>a3</name> > </item> > </list> > > > _______________________________________________ > > > Rails-spinoffs mailing list > > > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs > > > > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs > > >-- troels
First attempt was blocked by the mailing list for being too long, so
here''s the trimmed version. This works in IE also:
function xmlToHash(el) {
Element.cleanWhitespace(el);
if ((el.attributes && el.attributes.length > 0)
|| (el.hasChildNodes() && el.childNodes[0].nodeType == 1)) {
var localHash = {};
if (el.attributes && el.attributes.length >= 1) {
$A(el.attributes).each(function (attr) { localHash[attr.nodeName]
= [attr.nodeValue]; });
}
$A(el.childNodes).each(function (el) { xmlToHashElement(localHash,
el); });
for (attr in localHash) {
if (localHash[attr].length == 1) {
localHash[attr] = localHash[attr][0];
}
}
return localHash;
}
else {
return el.textContent || el.innerText || el.text || '''';
}
}
function xmlToHashElement (hash, el) {
if (el.nodeType == 2) {
hash[el.localName] = [ el.textContent || el.innerText || el.text ||
'''' ];
}
else {
var key = el.tagName;
if (hash[key]) {
hash[key].push(xmlToHash(el));
}
else {
hash[key] = [ xmlToHash(el) ];
}
}
}
________________________________
From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
[mailto:rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org]
On Behalf Of
Gregory Hill
Sent: Friday, March 17, 2006 11:27 AM
To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
Subject: RE: [Rails-spinoffs] good javascript xml parser
Oops... it doesn''t work at all in IE. Friggin IE.
_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs