Faced with a problem, tried to parse an XML document and replace the appropriate words to those that are in my array, decided to automate it and no idea what to do next. I've looked through a lot of information on this subject, but I haven't found the right answer.
const fs = require("fs")
const xmlParser = require("xml2json")
const formatXml = require("xml-formatter")
const wordReplacement = [ ["what"], ["yes"], ["please"], ["thanks"], ["because"], ["easy"], ["good"], ["this"], ["that"], ["them"], ["ok"], ["you"], ["ll"], ["telegram"] ];
const secondReplacement = [ ["wat"], ["ye"], ["pls"], ["thx"], ["cuz"], ["ez"], ["gud"], ["dis"], ["dat"], ["dem"], ["okie"], ["u"], ["ll"], ["meowgram"] ];
const startReplacement = [ "uwaaa! ", "ugu, ", "rawr " ]
const endReplacement = [ [" OwO"], [" UwU"], [" :3"], [" xD"], [" <3"], [" o.o"], [" >w<"], [" wyaaaa"] ]
// fs.readFile( "./dataMain.xml", function(err, data) {
// const xmlObj = xmlParser.toJson(data, {reversible: true, object: true})
// console.log(xmlObj)
// })
fs.readFile( "./dataMain.xml", function(err, data) {
const xmlObj = xmlParser.toJson(data, {reversible: true, object: true})
const resources = xmlObj["resources"]["string"]
for (let i = 0; i < resources.length; i ) {
if (resources[i]) {
// xmlObj["resources"]["string"][i].$t = xmlObj["resources"]["string"][i].$t.toLowerCase(); // trying to make it lower case
if (xmlObj["resources"]["string"][i].$t == wordReplacement[i]) {
// xmlObj["resources"]["string"][i].$t = xmlObj["resources"]["string"][i].replace(wordReplacement[i], secondReplacement[i]).$t; // trying to replace words
}
}
}
const stringifiedXmlObj = JSON.stringify(xmlObj)
const finalXml = xmlParser.toXml(stringifiedXmlObj)
fs.writeFile("./dataMain.xml", formatXml(finalXml, {collapseContent: true}), function(err, result) {
if (err) {
console.log("houston, we got a problem")
} else {
console.log("we did it, somehow")
}
})
})
CodePudding user response:
Mapping XML to JSON and back can result in a different XML structure - especially if you have multiple sibling elements with the same name or mixed child nodes.
I suggest editing the XML directly using DOM Xpath. The xmldom and xpath packages add the same APIs that are available in browsers.
Here is a basic example on how to fetch and modify nodes:
// load into DOM
const dataDocument = (new DOMParser()).parseFromString(
getXMLString(), 'text/xml'
);
// fetch the "string" elements into a snapshot
// this can be adapted according to the XML structure
const snapShot = dataDocument.evaluate(
'/resources/string',
dataDocument,
null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
null
);
// iterate the fetched elements
for (let i = 0; i < snapShot.snapshotLength; i ) {
const stringNode = snapShot.snapshotItem(i);
// modify the node text content
stringNode.textContent = modifyString(stringNode.textContent);
}
// serialize DOM document into a string
console.log(
(new XMLSerializer()).serializeToString(dataDocument)
);
function modifyString(input) {
return input.toLowerCase();
}
function getXMLString() {
return "<resources><string>FOO</string></resources>";
}
With this you can implement the required string modifications in modifyString()
.