Hi folks,
I have a fairly complex xml document which looks like this:
<my:InsideVie w xmlns:my="http://schemas.microso ft.com/office/infopath/2003/myXSD/2004-08-02T14:22:59"
xml:lang="en-us">
<my:Title>Tit le in here</my:Title>
<my:StandFirst> Standfirst in here</my:StandFirst>
<my:RelatedMate rial>
<my:Image>/images/InsideView/norma.gif</my:Image>
<my:EmphasisedT ext>picture of norma</my:EmphasisedTe xt>
<my:AlternateTe xt>alternate text</my:AlternateTex t>
<my:PlaceHolder Tag><!-- image one --></my:PlaceHolderT ag>
</my:RelatedMater ial>
<my:RelatedMate rial>
<my:Image>/images/InsideView/elena.gif</my:Image>
<my:EmphasisedT ext>another picture of elena</my:EmphasisedTe xt>
<my:AlternateTe xt></my:AlternateTex t>
<my:PlaceHolder Tag><!-- image two --></my:PlaceHolderT ag>
</my:RelatedMater ial>
<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:RelatedLink s>
<my:URL>http://www.google.com</my:URL>
<my:URLText>thi s is a link to google</my:URLText>
</my:RelatedLinks >
<my:RelatedLink s>
<my:URL>http://www.bbc.co.uk</my:URL>
<my:URLText>thi s 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 <RelatedMateria l> node, and see if any of the tags placed
in <my:PlaceHolder Tag> 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.XMLDO M");
xmldoc.async = false;
xmldoc.load(xml Doc);
strBody = xmldoc.getEleme ntsByTagName("B ody");
alert(strBody.l ength); // DISPLAYS 0
alert(strBody[0].firstChild.nod eValue); // error: object expected
myNodes = _XML.getElement sByTagName("Rel atedMaterial");
alert(myNodes.l ength) // displays 0
//Extract the different values using a loop.
for( var counter = 0; counter < myNodes.length; counter++ ) {
display += myNodes.item(co unter).firstChi ld.nodeValue + "\n";
}
alert(display) // error: undefined
document.all.ma in.innerHTML = display;
}
I have a fairly complex xml document which looks like this:
<my:InsideVie w xmlns:my="http://schemas.microso ft.com/office/infopath/2003/myXSD/2004-08-02T14:22:59"
xml:lang="en-us">
<my:Title>Tit le in here</my:Title>
<my:StandFirst> Standfirst in here</my:StandFirst>
<my:RelatedMate rial>
<my:Image>/images/InsideView/norma.gif</my:Image>
<my:EmphasisedT ext>picture of norma</my:EmphasisedTe xt>
<my:AlternateTe xt>alternate text</my:AlternateTex t>
<my:PlaceHolder Tag><!-- image one --></my:PlaceHolderT ag>
</my:RelatedMater ial>
<my:RelatedMate rial>
<my:Image>/images/InsideView/elena.gif</my:Image>
<my:EmphasisedT ext>another picture of elena</my:EmphasisedTe xt>
<my:AlternateTe xt></my:AlternateTex t>
<my:PlaceHolder Tag><!-- image two --></my:PlaceHolderT ag>
</my:RelatedMater ial>
<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:RelatedLink s>
<my:URL>http://www.google.com</my:URL>
<my:URLText>thi s is a link to google</my:URLText>
</my:RelatedLinks >
<my:RelatedLink s>
<my:URL>http://www.bbc.co.uk</my:URL>
<my:URLText>thi s 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 <RelatedMateria l> node, and see if any of the tags placed
in <my:PlaceHolder Tag> 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.XMLDO M");
xmldoc.async = false;
xmldoc.load(xml Doc);
strBody = xmldoc.getEleme ntsByTagName("B ody");
alert(strBody.l ength); // DISPLAYS 0
alert(strBody[0].firstChild.nod eValue); // error: object expected
myNodes = _XML.getElement sByTagName("Rel atedMaterial");
alert(myNodes.l ength) // displays 0
//Extract the different values using a loop.
for( var counter = 0; counter < myNodes.length; counter++ ) {
display += myNodes.item(co unter).firstChi ld.nodeValue + "\n";
}
alert(display) // error: undefined
document.all.ma in.innerHTML = display;
}
Comment