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

106
Views
Jerarquía de objetos del documento XML usando JavaScript

Dado un documento XML como este:

 <Root> <Child1> <ChildOfChild1_1> <FinalChild> </FinalChild> </ChildOfChild1_1> <ChildOfChild1_2> </ChildOfChild1_2> </Child1> <Child2> <ChildOfChild2_1> </ChildOfChild2_1> </Child2> <Child3> </Child3> </Root>

Me gustaría devolver un objeto que tiene solo dos parámetros, nombre e hijos. Se vería como:

 { name: "Root" children: [ { name: "Child1" children: [ { name: "ChildOfChild1_1" children:[ { name: "FinalChild" children: null } ] }, { name: "ChildOfChild1_2" children: null } ] }, { name: "Child2" children: [ { name: "ChildOfChild2_1" children: null } ] }, { name: "Child3" children: null }, ] }

No me importan los atributos de los nodos, solo el nombre del nodeName y si tienen hijos. Escribí el código para obtener el primer nivel de niños, pero no puedo entender la parte recursiva para profundizar tanto como sea necesario. Gracias.

A continuación se muestra lo que tengo hasta ahora:

 //assuming block is the root node and the root will at least have 1 child let root = { name = '', children = [] }; root.name = block.nodeName; for(let i=0 ; i<block.children.length ; i++) { root.children.push(getChildren(block.children[i])); } function getChildren(data) { let child = {}; child.name = data.nodeName; child.children = []; //stuck here return child; }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

La tarea es mucho más sencilla de lo que crees.

Desea una función que devuelva un nombre de nodo y la lista de nodos secundarios, cada uno de ellos procesado exactamente de la misma manera:

 function toObject(node) { return { name: node.nodeName, children: [...node.children].map(toObject) }; }

Eso es todo.

 const xmlDoc = new DOMParser().parseFromString(`<Root> <Child1> <ChildOfChild1_1> <FinalChild> </FinalChild> </ChildOfChild1_1> <ChildOfChild1_2> </ChildOfChild1_2> </Child1> <Child2> <ChildOfChild2_1> </ChildOfChild2_1> </Child2> <Child3> </Child3> </Root> `, "application/xml"); function toObject(node) { return { name: node.nodeName, children: [...node.children].map(toObject) }; } const tree = toObject(xmlDoc.documentElement); console.log(tree);

Si realmente quiere null cuando no hay hijos (una matriz vacía es mucho más fácil de manejar en el futuro, confíe en mí), estoy seguro de que puede hacer el cambio necesario en toObject usted mismo.

about 4 years ago · Juan Pablo Isaza Report
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!