Home > database >  How to parse and append to XML file a new content?
How to parse and append to XML file a new content?

Time:07-03

I have an hardcoded XML file I need to parse and append some content.

This is the file:

<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="PropertiesComponent"><![CDATA[{
  "keyToString": {
    "RunOnceActivity.OpenProjectViewOnStart": "true"
  }
}]]></component>
</project>

My Target is to convert it to:

<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="PropertiesComponent"><![CDATA[{
  "keyToString": {
    "RunOnceActivity.OpenProjectViewOnStart": "true",
    "test": 5
  }
}]]></component>
</project>

I have the following JS code to do so:

const path = require('path');

const fs = require('fs-extra');
const xml2js = require('xml2js');

const xmlFilePath = path.join(__dirname, './resource.xml');
const xmlFileDestPath = path.join(__dirname, './resource-new.xml');

(async () => {
    const xmlContent = await fs.readFile(xmlFilePath, 'utf-8').catch(() => '');
    const parsedXml = await xml2js.parseStringPromise(xmlContent);

    const objectToAppend = parsedXml['project']['component'][0]['_'];
    const parsedObject = JSON.parse(objectToAppend);
    const parsedKeyToString = parsedObject['keyToString'];

    const newObject = { ...parsedKeyToString, test: 5 };

    parsedXml['project']['component'][0]['_'] = JSON.stringify({ keyToString: newObject });

    const builder = new xml2js.Builder({
        xmldec: { version: '1.0', encoding: 'UTF-8' },
        cdata: true,
    });

    const workspaceXmlFileContent = builder.buildObject(parsedXml);

    await fs.writeFile(xmlFileDestPath, workspaceXmlFileContent);
})();

But the output file is:

<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="PropertiesComponent">{"keyToString":{"RunOnceActivity.OpenProjectViewOnStart":"true","test":5}}</component>
</project>

Not exactly what I wanted. Where is my problem?

CodePudding user response:

I tried your case with xml2js and got the same error.

I fixed it by using another package xml2j-cdata This is my new test and it works just the way you want it to. Hope can help you https://codesandbox.io/s/xml2js-cdata-test-hu39jw?file=/src/index.js https://www.npmjs.com/package/xml2js-cdata

CodePudding user response:

I could solve it by inserting a dummy value which enforces the library to use CDATA. I created dummy value that contains a character (<) which will trigger the package to use CDATA:

const path = require('path');

const fs = require('fs-extra');
const xml2js = require('xml2js');

const xmlFilePath = path.join(__dirname, './resource.xml');
const xmlFileDestPath = path.join(__dirname, './resource-new.xml');

(async () => {
    const xmlContent = await fs.readFile(xmlFilePath, 'utf-8').catch(() => '');
    const parsedXml = await xml2js.parseStringPromise(xmlContent);

    const objectToAppend = parsedXml['project']['component'][0]['_'];
    const parsedObject = JSON.parse(objectToAppend);
    const parsedKeyToString = parsedObject['keyToString'];

    const newObject = { ...parsedKeyToString, test: '<xxx>' };

    parsedXml['project']['component'][0]['_'] = JSON.stringify({ keyToString: newObject });

    const builder = new xml2js.Builder({
        xmldec: { version: '1.0', encoding: 'UTF-8' },
        cdata: true,
    });

    const workspaceXmlFileContent = builder.buildObject(parsedXml);

    await fs.writeFile(xmlFileDestPath, workspaceXmlFileContent);
})();
  • Related