I've been trying to read XML file, and change the value of one of its tags. My main goal in the future is to take a docx file and change it's header. I'm looking for a simple solution and testing on XML file.
I tried using xhr2, installed with npm, At first xhr2 was was throwing errors as not recognized, but after looking for that issue I still have a warning under require('xhr2') -
Could not find a declaration file for module 'xhr2'. 'Desktop/jsXml/node_modules/xhr2/lib/xhr2.js' implicitly has an 'any' type.
Try `npm i --save-dev @types/xhr2` if it exists or add a new declaration (.d.ts) file containing `declare module 'xhr2';
and I get a different error now -
Desktop/jsXml/node_modules/xhr2/lib/xhr2.js:281
throw new NetworkError(`Unsupported protocol ${this._url.protocol}`);
Tried to look for it without clear result. I don't mind using a different library, anything can work as long as it will be simple. Here is my code, not sure if I'm using something the wrong way. any suggestions would be appreciated, thanks!
function loadXMLDoc() {
var XMLHttpRequest = require('xhr2');
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xmlhttp.open("GET", "header1.xml", true);
xmlhttp.send();
}
function myFunction(xml) {
var x, i, xmlDoc, txt;
xmlDoc = xml.responseXML;
txt = "header: ";
x = xmlDoc.getElementsByTagName("w:t");
console.log("txt");
for (i = 0; i < x.length; i++) {
txt += x[i].childNodes[0].nodeValue;
}
console.log("txt");
}
loadXMLDoc();
Also, here is my XML file:
<w:hdr xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:sl="http://schemas.openxmlformats.org/schemaLibrary/2006/main" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture" xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart" xmlns:lc="http://schemas.openxmlformats.org/drawingml/2006/lockedCanvas" xmlns:dgm="http://schemas.openxmlformats.org/drawingml/2006/diagram" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml">
<w:p w:rsidR="00000000" w:rsidDel="00000000" w:rsidP="00000000" w:rsidRDefault="00000000" w:rsidRPr="00000000" w14:paraId="00000002">
<w:pPr>
<w:rPr/>
</w:pPr>
<w:r w:rsidDel="00000000" w:rsidR="00000000" w:rsidRPr="00000000">
<w:rPr>
<w:rtl w:val="0"/>
</w:rPr>
<w:t xml:space="preserve">This will be our new header.</w:t>
</w:r>
</w:p>
</w:hdr>