Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    May 2019
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Get subset of DOM Parser XML

    I have a variable containing parsed XML (disclaimer - Javascipt level is beginner here).:

    Code:
     function xmlDocFunction(xml) { 
            parser = new DOMParser(); 
            return parser.parseFromString(xml, "text/xml"); 
        } 
    
    xmlDoc = xmlDocFunction(UserDefinedJavascript.DocumentSetXML)
    Now I am trying to change someone else's code without trying to rewrite... So, a fairly complicated script then ensues whereby a list of fields is obtained from an HTML form. Two arrays are created "names" which is all the column names from the XML Document and includeList which is the names of all the input fields within the form.

    The script then uses a jquery $.each and parses through the arrays to do various stuff.

    They key bit is that for each name in the name array textContent is taken from the xmldoc and put into the field:

    Code:
    $('[name="' + liveNames + "]').val(cleanText(xmlDoc.getElementsByTagName(name)[0].textContent)).css({"border": "solid 2px green"
                            }).addClass("ImportHighlight")
    My problem is that within the XML the column names repeat but in different parent nodes:
    Code:
      <root>
        <cp1>
        <Tfield1>My test</Tfield1>
        <Tfield2>another line</Tfield>
        </cp1>
        <cp2>
        <Tfield1>second test</Tfield1>
        <Tfield2>second line</Tfield>
        </cp2>
        <cp3>
        <Tfield1>third test</Tfield1>
        <Tfield2>third line</Tfield>
        </cp3>
        </root>
    so, if name="Tfield1" when the script executes getElementsByTagName(name) I need a way of specifying that it should only search within "cp2" for example (the node name is captured earlier as PlanType.

    So how to search for names within a node?

  2. #2
    Senior Coder
    Join Date
    Aug 2010
    Posts
    1,282
    Thanks
    46
    Thanked 280 Times in 278 Posts
    the xml you have posted is invalid
    I have corrected your xml …
    Code:
    <!DOCTYPE html>
    <html>
    <body>
    <div id=result></div>
    <textarea id=ta  rows=27 cols=24>
    <root>
     <cp1>
      <Tfield1>
       My test
      </Tfield1>
      <Tfield2>
       another line
      </Tfield2>
     </cp1>
     <cp2>
      <Tfield1>
       second test
      </Tfield1>
      <Tfield2>
       second line
      </Tfield2>
     </cp2>
     <cp3>
      <Tfield1>
       third test
      </Tfield1>
      <Tfield2>
       third line
      </Tfield2>
     </cp3>
    </root>
    </textarea>
    <script>
    var parser, xmlDoc;
    parser = new DOMParser();
    xmlDoc = parser.parseFromString(ta.textContent,"text/xml");
    result.innerHTML =
    xmlDoc.getElementsByTagName("cp2")[0].getElementsByTagName("Tfield1")[0].firstChild.nodeValue;
    </script>
    </body>
    </html>
    Last edited by DaveyErwin; May 8th, 2019 at 11:32 PM.

  3. #3
    New to the CF scene
    Join Date
    May 2019
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, yes I was a bit slapdash with the example XML.

    Thank you very much for the solution. I didn't know you could chain two getelementsbyTagName in that way.


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •