XML parser and javascript function

Discussion in 'XML' started by Leila, Aug 11, 2004.

  1. Leila

    Leila Guest

    Hi folks,

    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>&lt;!-- image one --&gt;</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>&lt;!-- image two --&gt;</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 &lt;!-- image one --&gt;
    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;
    }
     
    Leila, Aug 11, 2004
    #1
    1. Advertising

  2. Leila wrote:


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


    There is a </my:image> missing here, otherwise the document is not
    well-formed.

    > <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>


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

    var xmlDocument = new ActiveXObject('Msxml2.DOMDocument');
    xmlDocument.async = false;
    var loaded = xmlDocument.load('test2004081101.xml');
    if (loaded) {
    xmlDocument.setProperty('SelectionLanguage', 'XPath');
    xmlDocument.setProperty('SelectionNamespaces',
    'xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2004-08-02T14:22:59"');
    var relatedMaterial =
    xmlDocument.selectSingleNode('//my:RelatedMaterial');
    alert(relatedMaterial);
    }

    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.setProperty('SelectionLanguage', 'XPath');
    xmlDocument.setProperty('SelectionNamespaces', '...')

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Aug 11, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ZOCOR

    XML Parser VS HTML Parser

    ZOCOR, Oct 3, 2004, in forum: Java
    Replies:
    11
    Views:
    842
    Paul King
    Oct 5, 2004
  2. arne
    Replies:
    0
    Views:
    376
  3. Erik Wasser
    Replies:
    5
    Views:
    515
    Peter J. Holzer
    Mar 5, 2006
  4. Sean
    Replies:
    3
    Views:
    338
    robic0
    Oct 3, 2006
  5. Sean
    Replies:
    0
    Views:
    392
Loading...

Share This Page