Simple - Getting Attribute from XML

83 views
Skip to first unread message

awesome

unread,
Aug 7, 2012, 3:50:23 PM8/7/12
to greasemon...@googlegroups.com
Hi..

I'm an idiot, and i'm extremely stupid, not to mention an Economics major.. meaning i've never formally studied any programming..

that said, could you please help me get an attribute's value form an xml file?

Sample XML from w3c:

<book>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year><price>30.00</price>
</book>

What i want to do is grab the value "en" written under the lang attribute for the title tag.. plis, plis, plis.. i googled, cut paste a billion different versions of javascript codes, and have been at it for 8 hours now.. i've been using alert() for debugging and it everything upto the XML parse works!

Valentine S.

unread,
Aug 7, 2012, 4:08:12 PM8/7/12
to greasemon...@googlegroups.com
1. Your XML-file has incorrect format (<book> without </book>)
2. How do you parse: regular expression or DOM? Where is this XML-file stored: string or smth else?
3. Do you use jQuery?

2012/8/7 awesome <aminsha...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "greasemonkey-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/greasemonkey-users/-/Lncz1bVch0QJ.
To post to this group, send email to greasemon...@googlegroups.com.
To unsubscribe from this group, send email to greasemonkey-us...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/greasemonkey-users?hl=en.



--
Валентин Сарычев, +79629250516
http://valentine-sarychev.moikrug.ru/

awesome

unread,
Aug 7, 2012, 4:50:54 PM8/7/12
to greasemon...@googlegroups.com
sorry.. i lol, what i meant was:


<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>

This isn't the XML file i'm trying to parse, that has sensitive data, but i can assure you it's structured properly.. it's stored online on my server, i've already set the XML's url as a variable called path_to_xml, and i honestly have no idea if i'm using a regular expression or DOM.. I tried modifying the sample code from the GM_xmlhttpRequest wiki page here. It didn't work.. and no, i'm not using JQuery.

Thank you for the reply!

Valentine S.

unread,
Aug 7, 2012, 6:29:19 PM8/7/12
to greasemon...@googlegroups.com
1. Show your code.
2. Did you manage to get the data?

2012/8/8 awesome <aminsha...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "greasemonkey-users" group.

To post to this group, send email to greasemon...@googlegroups.com.
To unsubscribe from this group, send email to greasemonkey-us...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/greasemonkey-users?hl=en.

Valentine S.

unread,
Aug 7, 2012, 7:03:18 PM8/7/12
to greasemon...@googlegroups.com
When you get the data, then parse:
var lang = new DOMParser().parseFromString(response.responseText, "text/xml").getElementsByTagName('title')[0].getAttribute('lang');

GM_xmlhttpRequest({
  method: "GET",
  url: "http://www.example.net/",
  headers: {
    "User-Agent": "Mozilla/5.0",    // If not specified, navigator.userAgent will be used.
    "Accept": "text/xml"            // If not specified, browser defaults will be used.
  },
  onload: function(response) {
    // Inject responseXML into existing Object (only appropriate for XML content).
    if (!response.responseXML) {
      response.responseXML = new DOMParser()
        .parseFromString(response.responseText, "text/xml");
    } 

    // create response.lang
    response.lang = response.responseXML.getElementsByTagName('title')[0].getAttribute('lang'); 

    GM_log([
      response.status,
      response.statusText,
      response.readyState,
      response.responseHeaders,
      response.responseText,
      response.finalUrl,
      response.responseXML,
      response.lang,
    ].join("\n"));
  }
});

2012/8/8 Valentine S. <diz...@gmail.com>

awesome

unread,
Aug 14, 2012, 4:17:00 AM8/14/12
to greasemon...@googlegroups.com
You sir are a genius! Thank you! I'm sorry i couldn't reply earlier, but my script works now, perfectly! Thanks to you! No more finding information by manually sorting through the eight hundred or so XML files for me, no sir, all thanks to you!
Reply all
Reply to author
Forward
0 new messages