Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

142
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda