0

I'm using a class to convert xml to json for my project like so:

export class AccessionClass {
  constructor(accessionFilename) {
    this.accessionFilename = accessionFilename
    this.accessionXML = fs.readFileSync(this.accessionFilename).toString()
    const parser = new xml2js.Parser({ explicitArray: false })
    parser.parseString(this.accessionXML, (err, results) => {
      this.accessionJSON = results.accessions
    })...
  getJSONItem(itemNumber, callback) {
    let oneItem
    let items = this.accessionJSON.item.filter(item => {
      return item.accession === itemNumber
    })
    if (items.length > 1) {
      console.log('getJSONItem: ' + items.length + ' items for accession: ' + itemNumber)
    }
    callback(items[0])
  }

After calling getJSONItem the JSON string values have unconverted entities

(' & ...)

Am I missing an obvious option to do this? What is the best way to get these converted?

Marvin Budd
  • 431
  • 2
  • 2
  • 1
    Please may you share some example XML and the output you want? – evolutionxbox Mar 18 '22 at 16:50
  • Your XML might container another XML or HTML fragment or document. If that is the case you should parse it as separate fragment or document. – ThW Mar 18 '22 at 16:51
  • Here is a description that results from the above: ``` Guessing on Wilda's family. Picture was labelled "Aunts, Uncles, Grandparents, cousins, Mother and Father, brothers and sisters. ``` The XML is too long to include here. But here is one item. There must be two items for the code provided to function. ``` Guessing on Wilda's family. Picture was labelled "Aunts, Uncles, Grandparents, cousins, Mother and Father, brothers and sisters. 45 – Marvin Budd Mar 18 '22 at 19:58
  • ``` The XML is too long to include here. But here is one item. There must be two items for the code provided to function. ``` Guessing on Wilda's family. Picture was labelled "Aunts, Uncles, Grandparents, cousins, Mother and Father, brothers and sisters. 45 ``` I'm hand typeing some of this, but you get an idea. – Marvin Budd Mar 18 '22 at 20:05
  • I had invalid entitys in my original xml. Not sure how that happened. Wilda's is obviously corrupted. After changing it to Wilda's it worked fine. I thought xml2js was at fault when it was really invalid data. – Marvin Budd Mar 19 '22 at 20:45

2 Answers2

0

On closer inspection of the above you may observe that the entities were munged by something in my xml. Fixing that solved my problem! I'll just have to hand edit them to fix them. Sorry for the false alarm.

Marvin Budd
  • 431
  • 2
  • 2
-1

I know there's no other way you're going to do this with your hand to organize it.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 19 '22 at 01:54