Question

So I am switching the graphML import method in one of my scripts and I am having trouble cleaning up my imported data. Currently, the graphML data looks like this:

<node id="1">
  <data key="url">www.someURL.com</data> 
  <data key="label">Guy, This</data> 
  <data key="profile_url">/dir/uri=http...</data> 
  <data key="number_of_authored_works">x</data> 
  <data key="earliest_publication">y</data> 
  <data key="num_earliest_publication">z</data> 
  <data key="latest_publication">a</data> 
  <data key="num_latest_publication">b</data> 
  <data key="num_unknown_publication">c</data> 

Here is my code as it stands:

function loadXML() {
$.ajax({
    type: "GET",
    url: "Scripts/coauthor4.xml",
    dataType: "xml",
    success: xmlParser
})}

var someVar= [];
function xmlParser(xml) {

$(xml).find('edge').each(function (i) {
    someVar[i] = $(this).find('data').siblings().text();
})}

When I go to the console and ask for a node (someVar[i]), I get something like this:

www.someURL.comGuy, This/dir/uri=http...xyzabc

So I have all of the necessary data atleast...good. But I want to make key:value pairs out of each data point. So essentially, someVar[i][0] should be this:

someVar[i][0]
  url: 'www.someURL.com'
Was it helpful?

Solution

Unless I'm missing something, you just need to toss another loop in there. Iterate through each 'data' node:

$(xml).find('edge').each(function (i) {
    $(this).find('data').each(function(j) {
        var key = $(this).attr('key');
        someVar[i] = someVar[i] || {};
        someVar[i][key] = $(this).text();
    });
});

Now someVar[0] looks like:

{
    url: 'www.someURL.com',
    label: 'Guy, This'
    ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top