0

I have a jQuery script in my HTML that parses out the URL name. Is it possible to use that parsed out name and query an XML where an element's attribute is the site's name?

<sites>
<site name="Blah00"> 
    <systemStatus color="green">Normal</systemStatus>
    <networkNotes>System status is normal</networkNotes>
</site>

<site name="Blah01">
    <systemStatus color="green">Normal</systemStatus>
    <networkNotes>System status is normal</networkNotes>
</site>

   ....
  </sites>

For example, the site for the first <site> in my XML could be http://www.example.com/status-Blah00. Inside the HTML for that site, I am pulling out the Blah00 of the URL and storing that in a variable.

How can I use that variable to get data from my XML file?

For example, under an area of my site that displays the systemStatus, I want to display the <systemStatus> of the site whose attribute name is the same as the variable (in this case, Blah00).

As helderdarocha pointed out, I can get a site by doing:

var note = $("site[name='Blah01'] networkNotes");
alert(note.text());

However I am asking how I can basically do that same thing but instead of saying the name of the site (ie: Blah00, Blah01, etc.) I want to use the variable that I have pulling the site name from the actual URL.

From my HTML:

<script>
  function myExampleSite() 
  {
    var myURL = window.location.href;
    var dashIndex = myURL.lastIndexOf("-");
    var dotIndex = myURL.lastIndexOf(".");
    var result = myURL.substring(dashIndex + 1, dotIndex);
    return result;
  }

  var siteName = myExampleSite();
</script>

I want to be able to use siteName instead of explicitly typing Blah00, Blah01, etc.

Mkalafut
  • 729
  • 2
  • 13
  • 34
  • jQuery is just a JavaScript library mainly useful for DOM processing and AJAX. I am not aware of any XML processing features of jQuery nor have I heard of the concept of "jQuery variables". A variable is a language construct of JavaScript. If you want to process XML you will want an appropriate tool for that, google for "javascript xml library". – chiccodoro May 15 '14 at 14:29
  • 1
    @chiccodoro: jQuery can work with XML files just fine. Heck, so can vanillaJS. See http://api.jquery.com/jquery.parsexml/ and https://developer.mozilla.org/en-US/docs/Parsing_and_serializing_XML – gen_Eric May 15 '14 at 14:29
  • possible duplicate of [Is there any easy to use Javascript XML library recommend?](http://stackoverflow.com/questions/1584043/is-there-any-easy-to-use-javascript-xml-library-recommend) – chiccodoro May 15 '14 at 14:30
  • @Rocket - you are right. In fact that leads me to more duplicates such as http://stackoverflow.com/questions/19220873/how-to-read-xml-file-contents-in-jquery-and-display-in-html-elements and http://stackoverflow.com/questions/7228141/how-to-parse-xml-using-jquery – chiccodoro May 15 '14 at 14:32
  • None of those came up in my quick search or on the side bar when writing this question. If this gets closed as a dupe, so be it but thank you either way for pointing those links out to me. At least they'll be helpful. That first thing that you suggested this question is a duplicate of is nowhere near what I am asking by the way, so maybe take it down a notch on the duplicate flagging. The two that you found in your 2nd comment are related, but not duplicates - but that first one is nowhere close. – Mkalafut May 15 '14 at 14:34
  • Here's an example: http://jsfiddle.net/helderdarocha/Vg528/ – helderdarocha May 15 '14 at 14:39
  • Wow that jsfiddle is very cool! (Only recently started working with javascript). Please refer to my question edit in a second, please. – Mkalafut May 15 '14 at 14:40
  • @Mkalafut: As far as I understand from your post your quetsion boils down to how you can process an XML file in order to get the element matching your string. The three links all tell you how you can process XML with Javascript, 2 of them by using jQuery. But don't be unsettled - it takes four more people to regard this as a duplicate before it gets closed. – chiccodoro May 15 '14 at 14:40
  • @chiccodoro I understand, and I was being genuine about those links being helpful (all except the first one, anyway). I have edited my question to be a bit more clear. – Mkalafut May 15 '14 at 14:44
  • 1
    `var note = $("site[name='" + siteName + "'] networkNotes");`? – chiccodoro May 15 '14 at 14:45
  • When I do that, I get [object Object] to display on the site and in the site debugger I see that note is "Blah00" - not the text inside of the node I want to display. Any ideas? – Mkalafut May 15 '14 at 14:58
  • @chiccodoro, if you make that an answer that'd be great. You're on to something there. – Mkalafut May 15 '14 at 15:02

0 Answers0