Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

139
Views
How to access tagName of custom node in DOM

I am reading a custom xml-file with custom tags such as <section></section> into a variable text. I then apply the following few lines of plain javascript:

parser = new DOMParser();
xmlDoc = parser.parseFromString(text, 'text/xml');
console.log(xmlDoc.documentElement);
parseDOM(xmlDoc.documentElement);

where

function parseDOM(node){
    type = node.nodeName;
    console.log(type);
    for(child in node.children){
        parseDOM(child);
    }
}

The console.log(xmlDoc.documentElement) part prints out the complete hierarchy without any problems, in particular with the tags having the correct names. Unfortunately the console.log(type) part prints out "undefined" for all my custom tags.

How can I test, whether the node at hand is of the custom type <section></section>? I was expecting node.nodeName == 'section' to work, but apparently it does not... And while we are at it, how can I test, whether the current node is a text-block?

Thank you very much for your support.

My goal is to replace the tags (e.g. turn <section> into <h2>), but also might do some heavier alterations to some tags in the future. I know that there very likely is some library perfectly suited for the job, but I am using this project to finally to get back writing some code, so I would prefer if the solution remains within standard javascript. Again, thank you very much for your help.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!