XML parser and javascript function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Leila

    #1

    XML parser and javascript function

    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>&lt;!-- image one --&gt;</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>&lt;!-- image two --&gt;</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 &lt;!-- image one --&gt;
    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;
    }
  • Martin Honnen

    #2
    Re: XML parser and javascript function



    Leila wrote:

    [color=blue]
    > 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>&lt;!-- image one --&gt;</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>&lt;!-- image two --&gt;</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 &lt;!-- image one --&gt;
    > to search and replace any of these tags with the image in
    > <my:image></my:Body>[/color]

    There is a </my:image> missing here, otherwise the document is not
    well-formed.
    [color=blue]
    > <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>[/color]

    If you have namespaces it is best to use selectSingleNod e/selectNodes
    after delaring prefixes for the namespaces as in the following example:

    var xmlDocument = new ActiveXObject(' Msxml2.DOMDocum ent');
    xmlDocument.asy nc = false;
    var loaded = xmlDocument.loa d('test20040811 01.xml');
    if (loaded) {
    xmlDocument.set Property('Selec tionLanguage', 'XPath');
    xmlDocument.set Property('Selec tionNamespaces' ,
    'xmlns:my="http ://schemas.microso ft.com/office/infopath/2003/myXSD/2004-08-02T14:22:59"');
    var relatedMaterial =
    xmlDocument.sel ectSingleNode('//my:RelatedMater ial');
    alert(relatedMa terial);
    }

    Note that XPath is only supported in MSXML 3 and later thus if you have
    IE 5 or IE 5.5 you might need to install MSXML 3 first on such a machine
    to make sure you are able to use
    xmlDocument.set Property('Selec tionLanguage', 'XPath');
    xmlDocument.set Property('Selec tionNamespaces' , '...')

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/

    Comment

    Working...