1

I am pretty new to HTML and such and I am trying to create a HTML page that is completely 'self-contained' so all the CSS, data and JavaScript that it needs will be embedded in the HTML (I know this will create a very ugly HTML file, but I do not mind) I need to do this because I need to be able to send the HTML file to colleagues so they can open it without having to run a local server and without anything being uploaded on an online server.

For now, I've managed to add all the CSS and Javascript, but the Javascript uses ajax to load an external XML file with the data. I cannot seem to get this to work when I embed the XML into the HTML and try to load it through ajax

I've embedded the XML data inside the HTML like so:

<script id="graphFile" type="text/xmldata"> 
   <gexf>
      <graph defaultedgetype="directed" mode="static">
        <attributes class="node" mode="static">
          <attribute id="v_name" title="name" type="string"></attribute>
          <attribute id="indegree" title="In-Degree" type="integer">
            <default>0</default>
          </attribute>
      ...more data...
     </graph>
   </gexf>
</script>

In the javascript the original XML file was parsed like so (which I have taken from the example I am basing my own HTML on):

function loadGraph() {

    $.ajax({
        url: "graphFile.xml",
        dataType: "xml",
        success: function(data) {
           ...calculate stuff...
        }
   });
}

However, I have no idea how I can parse the data from the XML embedded in the HTML so the function(data) will still work

nadieh
  • 655
  • 1
  • 6
  • 19

1 Answers1

1

From XML parsing of a variable string in JavaScript

var xml = "<music><album>Beethoven</album></music>";

var result = $(xml).find("album").text();

OR you can step away from XML and into JSON, which would be my strong personal recommendation.

Community
  • 1
  • 1
Mark
  • 3,123
  • 4
  • 20
  • 31
  • I'm sorry but I cannot yet seem to make this work. Because there are already '"' present in the XML itself I cannot create the `var xml` because it will not be completely quoted – nadieh Dec 16 '13 at 19:23
  • @NBremer It also contains ' characters? A great excuse to ditch XML and get on the JSON bandwagon. Or if you are hard coding it anyways, just make JS objects out of the data you are trying to share. – Mark Dec 16 '13 at 19:26
  • OK, if that's the way to go. I used a JSON output of the file, but I did have to make quite some changes to the JavaScript function that restructured the XML data to accomodate the different structure of the JSON. But I got it working with the JSON version :) – nadieh Dec 16 '13 at 21:39
  • @NBremer I'll edit my answer to include the JSON suggestion and you can mark correct, glad I could help :) – Mark Dec 16 '13 at 21:40