/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Reading from XML file

Hi

I’m putting together a webpage for downloading a list of project files.

The project file names and status are listed in a seperate XML file. ie:

<CATALOG>
<ISSUE>
<DOCID></DOCID>
<REV></REV>
<TITLE></TITLE>
<PDF></PDF>
</ISSUE>

</CATALOG>

And read in from an html page with a bit of javascript like this: (extract)

for (var i=0;i<x.length;i++)
{ document.write(“<tr>”);
document.write(“<td><h6>”);
document.write(x[i].getElementsByTagName(“DOCID”)[0].childNodes[0].nodeValue);
document.write(“</td>”);
document.write(“<td><h6>”);
document.write(x[i].getElementsByTagName(“REV”)[0].childNodes[0].nodeValue);
document.write(“</td>”);
document.write(“<td><h4>”);
document.write(x[i].getElementsByTagName(“TITLE”)[0].childNodes[0].nodeValue);
document.write(“</td>”);
document.write(x[i].getElementsByTagName(“PDF”)[0].childNodes[0].nodeValue);
document.write(“</td>”);
document.write(“</tr>”);
}
document.write(“</table>”);
}

The <PDF></PDF> holds the file name to download. Currently the PDF file name is displayed but What i’d like to do is to instead of displaying the file name I’d like to show a download icon and make it a hyperlink to the file named in the <PDF></PDF> instead.

I guess I’d have to add what base directory to find the <PDF></PDF> files as well, rather than a full path to each file.

I’m sure this is possible, but how? Any advice would be great.

to post a comment
JavaScript

11 Comments(s)

Copy linkTweet thisAlerts:
@slaughtersApr 02.2008 — Replace this bit

[CODE]document.write("<td><h4>");
document.write(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
document.write("</td>");
document.write(x[i].getElementsByTagName("PDF")[0].childNodes[0].nodeValue);
document.write("</td>");[/CODE]


with this:

[CODE]var PDF = getElementsByTagName("PDF")[0].childNodes[0].nodeValue;
var Title = x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue;

document.write("<td><h4>");
document.write("<a href=" + PDF + ">" + Title + "</a>")
document.write("</td>");[/CODE]


and the title of the book becomes a hyper link to the PDF

If you wanted to use a Base directory just assign it to a variable and add that to the front of the PDF name:

[CODE]
var Base = "/somebasedirectory/";
var PDF = Base + getElementsByTagName("PDF")[0].childNodes[0].nodeValue;
var Title = x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue;

document.write("<td><h4>");
document.write("<a href=" + PDF + ">" + Title + "</a>")
document.write("</td>");[/CODE]
Copy linkTweet thisAlerts:
@__diff__authorApr 02.2008 — What can I say, many many thanks.
Copy linkTweet thisAlerts:
@__diff__authorApr 05.2008 — Hi there

I've had chance to try out the code but the hyperlinks aren't appearing, just blank space and the js stops loading the XML file content at that point.

Any help would be great.

Thanks
Copy linkTweet thisAlerts:
@slaughtersApr 05.2008 — What does it show if you do a "view source" ?
Copy linkTweet thisAlerts:
@__diff__authorApr 07.2008 — Hi

I Pm'd you with the code.

Thanks
Copy linkTweet thisAlerts:
@A1ien51Apr 07.2008 — <i>
</i>&lt;CATALOG&gt;
&lt;ISSUE&gt;
&lt;DOCID&gt;&lt;/DOCID&gt;
&lt;REV&gt;&lt;/REV&gt;
&lt;TITLE&gt;&lt;/TITLE&gt;
&lt;PDF&gt;&lt;/PDF&gt;
&lt;/ISSUE&gt;
&lt;/CATALOG&gt;


If that is your entire XML file, that is not valid XML.

Eric
Copy linkTweet thisAlerts:
@__diff__authorApr 07.2008 — Hi Eric

no this is the text xml file I'm using:

<?xml version="1.0" encoding="ISO-8859-1"?>

<CATALOG>

<ISSUE>

<DOCID>001</DOCID>

<REV>A</REV>

<TITLE>File 1</TITLE>

<PDF>001A.pdf</PDF>

</ISSUE>

<ISSUE>

<DOCID>002</DOCID>

<REV>A</REV>

<TITLE>File 2</TITLE>

<PDF>002A.pdf</PDF>

</ISSUE>

<ISSUE>

<DOCID>003</DOCID>

<REV>A</REV>

<TITLE>File 3</TITLE>

<PDF>003A.pdf</PDF>

</ISSUE>

</CATALOG>

Thanks
Copy linkTweet thisAlerts:
@A1ien51Apr 07.2008 — Another thing is you can not use document.wrote after the page initally loads. You should be using innerHTML or appendChild to add new content.

Use a tool like Firebug [www.getFirebug.com] to help you debug.

Eric
Copy linkTweet thisAlerts:
@__diff__authorApr 08.2008 — Thanks I'lll try Firebug out, at work I only have IE 7.0.

Not sure about this but I'm only wanting to load the data once when the page loads, I'm not looking for data to be appended in 'realtime'. ?

The document.write was to load the initial data once on opening the web page?

Just detaching content from presentation.

However you're the man to know so I will look at your recommendations. I'm only a novice.
Copy linkTweet thisAlerts:
@slaughtersApr 08.2008 — ']Hi

I Pm'd you with the code.

Thanks[/QUOTE]


Never got the PM.

At a guess I expect something is wrong with your anchor tag. Do your URL's contains spaces ? If so you may need to place single tic marks around the URL when you assign it to the "HREF=" attribute
Copy linkTweet thisAlerts:
@__diff__authorApr 17.2008 — At a guess I expect something is wrong with your anchor tag. Do your URL's contains spaces ? If so you may need to place single tic marks around the URL when you assign it to the "HREF=" attribute
[/QUOTE]


Thanks for all your help. Yes I did as you suggested and now all is working great. Project completed. :-)
×

Success!

Help @__diff__ spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...