Importing XML text


#1

I’ve just transferred from Edge where I used ajax to import xml text and place specific nodes into text boxes.
I’m trying to create a similar thing in Hype but I can’t get anything to work.
Does anyone have a working example they could share please?


#2

You can make use of JavaScript libraries to import XML content, but if you just want to import regular text, here’s one method: Importing content from a text file (.txt)


(Carlos Eduardo) #3

Don’t know if this helps you. But this generates an external JS that can be used to import text and such. I was looking for a solution similar to yours and this worked for me. So there you go. Don’t know if it helps all that much.

JSConteudoExterno.hype.zip (13.1 KB)


#4

Thanks Carlos your solution works well.


#5

Hi Daniel, The jQuery.parseXML( data ) works but my aim is to load external xml files.
Do you know how this could be achieved please?
Carlos’ solution works well but my clients aren’t keen to swap from XML.

Ive spent hours trying a multitude of solutions but to no avail, I’m sure I’m not alone in my plight.
Any help would be greatly appreciated


(Nick ) #6

Quick question:

Do you want to dynamically load XML copy from an external website?
And you want that copy to update on your Hype application as it is updated at the client site?

Thanks


#7

Hi Nick, Thank you for your reply.
My aim is to provide the client with a folder containing the Hype file and an xml file, that’s loaded dynamically, so they can make text alteration without touching Hype. They then upload the whole folder to their server.
I’d like to continue using CDATA to markup the xml if possible.
If the Hype application could check to see if the xml has been updated would be an added bonus.


(Hans-Gerd Claßen) #8

so what have you done so far ¿

provide a sample hypefile, provide a sample xml … and i’m sure (almost :wink: ) someone will step in :slight_smile:


#9

Here’s what I’ve done so far…
The text is loading from an external js file.
What I want is to load the text from the xml file
If anyone can make this happen I’d be extremely grateful and I’m sure it would benefit others searching for the same solution.
ImportTextTest.zip (20.5 KB)


(Hans-Gerd Claßen) #10

Hi,

what you’ll need is a XMLHttpRequest

var request = new XMLHttpRequest();
request.open('GET', '${resourcesFolderName}/text.xml', true);

request.onload = function() {
  if (request.status >= 200 && request.status < 400) {
    var yourXML = request.responseXML;
    //--> read textContent of your headlinetag
   console.log( yourXML.getElementsByTagName('headline')[0].textContent)
  } else {
    // error occured, do sthg.

  }
};

request.onerror = function() {
  // connection error, do sthg.
};

request.send();

fyi by default the request is async. if you want to avoid this set the last argument of the request to false


Xml url import of product details
#11

Thank you so much for your help with this problem I really appreciate it.
I pasted your code into my file but it doesn’t work, I’m not sure if I’m pasting it in the wrong place or it needs to be on a server to work, I’m testing it locally.
Could you post your working file please?


(Hans-Gerd Claßen) #12

Hi,

put your text.xml in the resources and run

var request = new XMLHttpRequest();
request.open('GET', '${resourcesFolderName}/text.xml', true);

request.onload = function() {
  if (request.status >= 200 && request.status < 400) {
    var yourXML = request.responseXML;
    
    
    loadTxt(yourXML);
  } else {
    // error occured, do sthg.

  }
};

request.onerror = function() {
  // connection error, do sthg.
};

request.send();



// LOAD TEXT FROM EXTERNAL FILE

function loadTxt(xmlObj)
{
hypeDocument.getElementById('heading').innerHTML = xmlObj.getElementsByTagName('headline')[0].textContent
hypeDocument.getElementById("txt0").innerHTML = '<font style="color:red">' + xmlObj.getElementsByTagName('txt0')[0].textContent + '</font>';
hypeDocument.getElementById("txt1").innerHTML = '<font style="color:black"><b>' + xmlObj.getElementsByTagName('txt1')[0].textContent + '</b></font>';
}

#13

Thank you Hans-Gerd,
I’d forgotten to add the xml to the resources. Part of the learning curve I suppose.
I really appreciate your help