Parsing Properties From Certain Nodes In Xml Using Javascript?
Suppose, I have an XML with descriptions of files and directories (where XML nodes of files that belong to some directory are nested into that directory's corresponding XML node):
Solution 1:
Using XPath e.g. //file[type = 'file']/data/offset
with either browser APIs like the DOM Level 3 XPath API evaluate
method on document nodes
const xml = `<list>
<signature>
hash...
</signature>
<file id="44">
<data>
<length>1759</length>
<offset>36491175</offset>
<size>1018</size>
<encoding style="application/x-gzip"/>
<extracted-checksum style="sha1">91e4e9a81d6b8abb07fd6009afe72178997c512b</extracted-checksum>
<archived-checksum style="sha1">f141cb115e21de3320c1afccd2dc115b78f83661</archived-checksum>
</data>
<type>file</type>
<name>dist.dat</name>
</file>
<file id="6">
<ctime>1970-01-01T00:00:00Z</ctime>
<mtime>1970-01-01T00:00:00Z</mtime>
<atime>1970-01-01T00:00:00Z</atime>
<group>wheel</group>
<gid>0</gid>
<user>root</user>
<uid>0</uid>
<mode>0700</mode>
<deviceno>0</deviceno>
<inode>0</inode>
<type>directory</type>
<name>src</name>
<file id="7">
<ctime>1970-01-01T00:00:00Z</ctime>
<mtime>1970-01-01T00:00:00Z</mtime>
<atime>1970-01-01T00:00:00Z</atime>
<group>wheel</group>
<gid>0</gid>
<user>root</user>
<uid>0</uid>
<mode>0700</mode>
<deviceno>0</deviceno>
<inode>0</inode>
<type>directory</type>
<name>Dede</name>
<file id="8">
<data>
<length>514</length>
<offset>36357051</offset>
<size>859</size>
<encoding style="application/x-gzip"/>
<extracted-checksum style="sha1">b13b86984f1ceeb698e879ad4a0c4174804529c3</extracted-checksum>
<archived-checksum style="sha1">414b16e24dbbbbf864d68bcde726d52d69a4dc04</archived-checksum>
</data>
<type>file</type>
<name>Hello.txt</name>
</file>
<file id="9">
<data>
<length>776</length>
<offset>36357665</offset>
<size>1630</size>
<encoding style="application/x-gzip"/>
<extracted-checksum style="sha1">318eec584b12f2333133d8d07bf6b2d883fa7070</extracted-checksum>
<archived-checksum style="sha1">c1d196e28e7a8ec0444f7d16f205bd67667f5eec</archived-checksum>
</data>
<type>file</type>
<name>Local_st.txt</name>
</file>
</file>
... More File Nodes
</file>
</list>`;
const domParser = newDOMParser();
const xmlDoc = domParser.parseFromString(xml, 'application/xml');
const result = xmlDoc.evaluate('//file[type = "file"]/data/offset', xmlDoc, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
console.log(result.snapshotLength);
const offsets = [];
for (let i = 0; i < result.snapshotLength; i++) {
offsets.push(result.snapshotItem(i).textContent);
}
console.log(offsets);
//alternativeconst offsetNumbers = [];
for (let i = 0; i < result.snapshotLength; i++) {
offsetNumbers.push(xmlDoc.evaluate('number()', result.snapshotItem(i), null, XPathResult.NUMBER_TYPE, null).numberValue);
}
console.log(offsetNumbers);
or using Saxon-JS 2 as a library you can use //file[type = 'file']/data/offset/number()
:
const xml = `<list>
<signature>
hash...
</signature>
<file id="44">
<data>
<length>1759</length>
<offset>36491175</offset>
<size>1018</size>
<encoding style="application/x-gzip"/>
<extracted-checksum style="sha1">91e4e9a81d6b8abb07fd6009afe72178997c512b</extracted-checksum>
<archived-checksum style="sha1">f141cb115e21de3320c1afccd2dc115b78f83661</archived-checksum>
</data>
<type>file</type>
<name>dist.dat</name>
</file>
<file id="6">
<ctime>1970-01-01T00:00:00Z</ctime>
<mtime>1970-01-01T00:00:00Z</mtime>
<atime>1970-01-01T00:00:00Z</atime>
<group>wheel</group>
<gid>0</gid>
<user>root</user>
<uid>0</uid>
<mode>0700</mode>
<deviceno>0</deviceno>
<inode>0</inode>
<type>directory</type>
<name>src</name>
<file id="7">
<ctime>1970-01-01T00:00:00Z</ctime>
<mtime>1970-01-01T00:00:00Z</mtime>
<atime>1970-01-01T00:00:00Z</atime>
<group>wheel</group>
<gid>0</gid>
<user>root</user>
<uid>0</uid>
<mode>0700</mode>
<deviceno>0</deviceno>
<inode>0</inode>
<type>directory</type>
<name>Dede</name>
<file id="8">
<data>
<length>514</length>
<offset>36357051</offset>
<size>859</size>
<encoding style="application/x-gzip"/>
<extracted-checksum style="sha1">b13b86984f1ceeb698e879ad4a0c4174804529c3</extracted-checksum>
<archived-checksum style="sha1">414b16e24dbbbbf864d68bcde726d52d69a4dc04</archived-checksum>
</data>
<type>file</type>
<name>Hello.txt</name>
</file>
<file id="9">
<data>
<length>776</length>
<offset>36357665</offset>
<size>1630</size>
<encoding style="application/x-gzip"/>
<extracted-checksum style="sha1">318eec584b12f2333133d8d07bf6b2d883fa7070</extracted-checksum>
<archived-checksum style="sha1">c1d196e28e7a8ec0444f7d16f205bd67667f5eec</archived-checksum>
</data>
<type>file</type>
<name>Local_st.txt</name>
</file>
</file>
... More File Nodes
</file>
</list>`;
const offsets = SaxonJS.XPath.evaluate(`parse-xml($xml)//file[type = 'file']/data/offset/number()`, [], { params : { xml : xml }});
console.log(offsets);
<scriptsrc="https://www.saxonica.com/saxon-js/documentation/SaxonJS/SaxonJS2.rt.js"></script>
Post a Comment for "Parsing Properties From Certain Nodes In Xml Using Javascript?"