Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

369
Vistas
How do I do a command like array.shift() to an XML string?

I have an XML like this stored in a String variable in JavaScript :

<metadata>
<a>...</a>
<b>...</b>
<c>...</c>
</metadata>

<data>
<a>...</a>
<b>...</b>
<c>...</c>
</data>

Since a valid XML can only have single root tag, mine cannot be called valid as it has two root tags: metadata & data. I would like to completely remove the metadata tag, as I don't have any use of it either.

I read about array.Shift() command, which removes the first element of an array. But since I have an XML, how do I do the same? Again, would just like to remove the <metadata> tag, so the result XML looks like this (given below).

<data>
<a>...</a>
<b>...</b>
<c>...</c>
</data>
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Use a DOMParser

const xmlString = `
<metadata>
  <a>...</a>
  <b>...</b>
  <c>...</c>
</metadata>
<data>
  <a>...</a>
  <b>...</b>
  <c>...</c>
</data>`;

const parser = new DOMParser();
const doc1 = parser.parseFromString(`<root>${xmlString}</root>`, "application/xml");
doc1.querySelector('metadata').remove()
console.log(doc1.documentElement.innerHTML)

about 4 years ago · Juan Pablo Isaza Denunciar

0

Since you talk about a string, just search for '<data>' and get the substring from that position to the end:

let str = `
    <metadata>
      <a>...</a>
      <b>...</b>
      <c>...</c>
    </metadata>
    
    <data>
      <a>...</a>
      <b>...</b>
      <c>...</c>
    </data>
    `

let data = str.substring(str.search('<data>'))

console.log(data)

Or search for '</metadata>' plus two new line characters to find the start of :

let data = str.substring(str.search('</metadata>\n\n') + 13)
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda