This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DOM sorting

 
Bob Green
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following xml file and need to be able to retrieve some data based on user input, how to go about it if I need to implement with javascript?
For instance, if the user wants to retrieve all the documents that belong to the ENGLISH department.

TIA,

<documents>
<document>
<id>A1</id>
<docType>DOC</docType>
<department>ENGLICH</department>
</document>
<document>
<id>T1</id>
<docType>DOC</docType>
<department>BUSINESS</department>
</document>
<document>
<document>
<id>P1</id>
<docType>DOC</docType>
<department>ECONOMY</department>
</document>
<document>
<id>A2</id>
<docType>PDF</docType>
<department>ENGLISH</department>
</document>
<document>
<id>A2</id>
<docType>PDF</docType>
<department>BUSINESS</department>
</document>
<document>
<id>A2</id>
<docType>PDF</docType>
<department>ECONOMY</department>
</document>
<document>
<id>A3</id>
<docType>TXT</docType>
<department>ENGLISH</department>
</document>
</documents>
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JavaScript does not have file I/O capabilities, so how would it get at the contents of this file?
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ulf Dittmer:
JavaScript does not have file I/O capabilities, so how would it get at the contents of this file?


Ulf you living under a stone? XMLHttpRequest?
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only way to do it is going to be a for loop that looks at the node text.

Eric
 
Bob Green
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for all your inputs. I am thinking about using the Document Object Model after parsing the xml to a DOM object.... Am I on the right track?

TIA
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Eric Pascarello:
Ulf you living under a stone? XMLHttpRequest?


XHR does files now? I thought it was some kind of HTTP thingy.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bob Lou:
Thank you for all your inputs. I am thinking about using the Document Object Model after parsing the xml to a DOM object.... Am I on the right track?

TIA


It is what I said you were going to have to do is use it ot loop through the values

something like
xmlDoc.getElementsByTagName("document")[0].childNodes[2].nodeValue;
or
xmlDoc.getElementsByTagName("document")[0].getElementsByTagName("department")[0].nodeValue;

Eric
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ulf Dittmer:


XHR does files now? I thought it was some kind of HTTP thingy.


I am just going to act like you are a on some sort of medication and do not have a clear mind! Yes you can grab XML, html, text, jsp, asp, .aspx, etc.

Eric
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Eric Pascarello:
I am just going to act like you are a on some sort of medication and do not have a clear mind!


That's generally a good assumption. Can't seem to get rid of that ringing in my ears anyways, so I might as well take another dose right now...
[ June 07, 2007: Message edited by: Ulf Dittmer ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic