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

361
Views
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 answers
Answer question

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 Report

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 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!