Created
March 2, 2020 08:03
-
-
Save huytc/a92307642ab9f6f53da17850ca1a5641 to your computer and use it in GitHub Desktop.
Get the differences between two XML structures: https://stackoverflow.com/questions/60481648/algorithm-js-or-pseudocode-to-get-difference-between-two-xml-trees
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const xml = require('xml-parse'); | |
// recursive function to build map | |
function buildMap(element, path, map) { | |
map[path] = {} | |
// const childElements = element.childNodes.filter(childNode => childNode.type === 'element'); | |
for (const childNode of element.childNodes) { | |
// skip text (because the xml-parse package also returns the unnecessary texts in an XML structure, e.g. line breaks) | |
if (childNode.type === 'text') continue; | |
// process child element | |
// add child element's name to indicate that this path has a child with this name | |
// use child element's type (dir/file) as the value | |
map[path][childNode.attributes.name] = childNode.tagName; | |
// if child element is dir, process it recursively | |
if (childNode.tagName === 'dir') buildMap(childNode, `${path}/${childNode.attributes.name}`, map); | |
} | |
} | |
// function to get the differences between two maps | |
function diffMaps(oldMap, newMap) { | |
const changes = []; | |
// traverse each path of the old map | |
for (const key of Object.keys(oldMap)) { | |
// get children in this path for both old map and new map | |
const oldChildren = oldMap[key]; | |
const newChildren = newMap[key]; | |
changes.push(...diffChildren(key, oldChildren, newChildren)); | |
} | |
return changes; | |
} | |
// function to get the differences between the children of two maps | |
function diffChildren(path, oldChildren, newChildren) { | |
const changes = []; | |
// traverse each child of the old children | |
for (const key of Object.keys(oldChildren)) { | |
// if new children also have that child ==> no change ==> remove that child from new children and continue | |
if (newChildren[key]) { | |
// the reason for deleting is that after we have deleted all the keys that are present in old children, the remaining keys in new children will be the newly added ones. | |
delete newChildren[key]; | |
continue; | |
} | |
// new children don't have that child ==> deleted ==> add to changes | |
const type = oldChildren[key]; | |
changes.push({ | |
node: type === 'dir' ? `<dir name="${key}">` : `<file name="${key}"/>`, | |
path: path, | |
change: 'deleted' | |
}); | |
} | |
// traverse each child of the new children and add them to changes | |
for (const key of Object.keys(newChildren)) { | |
const type = newChildren[key]; | |
changes.push({ | |
node: type === 'dir' ? `<dir name="${key}">` : `<file name="${key}"/>`, | |
path: path, | |
change: 'added' | |
}); | |
} | |
return changes; | |
} | |
// testing | |
const oldXmlString = String.raw` | |
<dir name="rootDir"> | |
<dir name="childDir"> | |
<file name="hello.jpg"/> | |
</dir> | |
<file name="linux.txt"/> | |
<file name="img.png"/> | |
</dir> | |
`.trim(); | |
const newXmlString = String.raw` | |
<dir name="rootDir"> | |
<dir name="childDir"> | |
<file name="hello.jpg"/> | |
<file name="interesting.brain"/> | |
</dir> | |
<dir name="photos"> | |
<file name="me.dng"/> | |
</dir> | |
<file name="img.png"/> | |
</dir> | |
`.trim(); | |
const oldXml = xml.parse(oldXmlString); | |
const newXml = xml.parse(newXmlString); | |
const oldRoot = oldXml[0]; | |
const newRoot = newXml[0]; | |
const oldMap = {}; | |
const newMap = {}; | |
buildMap(oldRoot, `/${oldRoot.attributes.name}`, oldMap); | |
buildMap(newRoot, `/${newRoot.attributes.name}`, newMap); | |
const diffs = diffMaps(oldMap, newMap); | |
console.log(diffs); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment