XML Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreXML Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 11th, 2004, 07:25 AM
1ei1a 1ei1a is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 32 1ei1a User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 28 m 13 sec
Reputation Power: 5
XML parser and javascript function

Hi folks,

Me again. More problems.

I have a fairly complex xml document which looks like this:

<my:InsideView xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2004-08-02T14:22:59" xml:lang="en-us">
<my:Title>Title in here</my:Title>
<my:StandFirst>Standfirst in here</my:StandFirst>
<my:RelatedMaterial>
<my:Image>/images/InsideView/norma.gif</my:Image>
<my:EmphasisedText>picture of norma</my:EmphasisedText>
<my:AlternateText>alternate text</my:AlternateText>
<my:PlaceHolderTag><!-- image one --></my:PlaceHolderTag>
</my:RelatedMaterial>
<my:RelatedMaterial>
<my:Image>/images/InsideView/elena.gif</my:Image>
<my:EmphasisedText>another picture of elena</my:EmphasisedText>
<my:AlternateText></my:AlternateText>
<my:PlaceHolderTag><!-- image two --></my:PlaceHolderTag>
</my:RelatedMaterial>
<my:Body>Main body text for newsletter. I want to place image one in here so I will use a placeholder, like this <!-- image one --> to search and replace any of these tags with the image in <my:image></my:Body>
<my:RelatedLinks>
<my:URL>http://www.google.com</my:URL>
<my:URLText>this is a link to google</my:URLText>
</my:RelatedLinks>
<my:RelatedLinks>
<my:URL>http://www.bbc.co.uk</my:URL>
<my:URLText>this is a link to </my:URLText>
</my:RelatedLinks>
</my:InsideView>

I am want to use my javascript function to load the XML document and work with the nodes within the document. My aim is to be able to loop through the <RelatedMaterial> node, and see if any of the tags placed in <my:PlaceHolderTag> can be found in the content within <my:Body>. If a placeholder is found, then I want to replace the placeholder in <my:Body> with the image in <my:image>.

Quite an ambitious task. Can anybody tell me what is wrong with my javascript function:

function loadXML(xmlDoc, divName) {

var display;
var xmldoc = new ActiveXObject("Microsoft.XMLDOM");
xmldoc.async = false;
xmldoc.load(xmlDoc);

strBody = xmldoc.getElementsByTagName("Body");

alert(strBody.length); // DISPLAYS 0

alert(strBody[0].firstChild.nodeValue); // error: object expected

myNodes = _XML.getElementsByTagName("RelatedMaterial");

alert(myNodes.length) // displays 0

//Extract the different values using a loop.
for( var counter = 0; counter < myNodes.length; counter++ ) {
display += myNodes.item(counter).firstChild.nodeValue + "\n";
}
alert(display) // error: undefined

document.all.main.innerHTML = display;
}

Reply With Quote
  #2  
Old August 11th, 2004, 12:40 PM
kid23 kid23 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 62 kid23 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
just wondering.. I haven't faced a similar issue (although JS & XMLDOM gave me some troubles), but when you use

strBody = xmldoc.getElementsByTagName("Body");

Shouldn't you be using the namespace as well ? (my:Body)

You first alert() returning a 0 is what you have to fix before trying to go any further. The rest seems ok. What you can also do is an alert right after xmldoc.load(xmlDoc);

This will show you exactly how the file has been loaded, and if the system is using tag names with or without namespaces.

Try this, and let me know if you need more help

Grtz

Reply With Quote
  #3  
Old August 11th, 2004, 01:01 PM
1ei1a 1ei1a is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 32 1ei1a User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 28 m 13 sec
Reputation Power: 5
Red face

Quote:
Originally Posted by kid23
just wondering.. I haven't faced a similar issue (although JS & XMLDOM gave me some troubles), but when you use

strBody = xmldoc.getElementsByTagName("Body");

Shouldn't you be using the namespace as well ? (my:Body)

You first alert() returning a 0 is what you have to fix before trying to go any further. The rest seems ok. What you can also do is an alert right after xmldoc.load(xmlDoc);

This will show you exactly how the file has been loaded, and if the system is using tag names with or without namespaces.

Try this, and let me know if you need more help

Grtz


Hi there!

Many thanks for your comments.

You are right: I do need to reference the namespace (ie my:Body) which I discovered soon after posting the problem. Thanks for pointing this out!

I am still having aproblem with the second part of the function, but I will carry on working at it and post any problems once I've tried everything else I can think of.

Once again, you've come through for me. THANKS

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreXML Programming > XML parser and javascript function


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT