英文:
how to access xml entries of type <Item> in an object
问题
以下是翻译好的部分:
如下所示的代码中,我正在处理一个tiff文件。最内层Promise中的img
对象包含以键值形式表示的一些数据。对象img.fileDirectory.GDAL_METADATA
包含以下发布的数据。
现在我的问题是,由于我想要访问STATISTICS_MAXIMUM
、STATISTICS_MEAN
等内容,我如何访问在GDALMetadata
中显示的内容?
GDALMetadata
<GDALMetadata>
<Item name="STATISTICS_MAXIMUM" sample="0">21.122838228436</Item>
<Item name="STATISTICS_MEAN" sample="0">1.2389914218174</Item>
<Item name="STATISTICS_MINIMUM" sample="0">-4.6630500033646</Item>
<Item name="STATISTICS_STDDEV" sample="0">2.0382729681586</Item>
</GDALMetadata>
代码:
response.on('close', async () => {
console.log('Retrieved all data');
readFile("./test-1.tiff")
.then((data) => {
dataAsArrayBuffer = data.buffer
fromArrayBuffer(dataAsArrayBuffer)
.then((geoTIFF) => {
geoTIFF.getImage()
.then((img) => {
console.log(img.fileDirectory.GDAL_METADATA);//<==============
console.log(img.getWidth(), img.getHeight(), img.getSamplesPerPixel());
})
.catch((e) => console.log("img.errorMessage:", e))
})
.catch((e) => console.log("geoTIFF.errorMessage:", e))
})
.catch((e) => console.log("data.errorMessage:", e))
});
英文:
as shown in the below posted code, i am processing a tiff file. the img
object in the inner-most promise contains some data in a form of key-value.
the object img.fileDirectory.GDAL_METADATA
contains the data posted below.
now my question is, as i would like to have access to STATISTICS_MAXIMUM
,STATISTICS_MEAN
,...etc. how can i have access to contents shown in GDALMetadata
GDALMetadata
<GDALMetadata>
<Item name="STATISTICS_MAXIMUM" sample="0">21.122838228436</Item>
<Item name="STATISTICS_MEAN" sample="0">1.2389914218174</Item>
<Item name="STATISTICS_MINIMUM" sample="0">-4.6630500033646</Item>
<Item name="STATISTICS_STDDEV" sample="0">2.0382729681586</Item>
</GDALMetadata>
code:
response.on('close', async()=>{
console.log('Retrieved all data');
readFile("./test-1.tiff")
.then((data)=>{
dataAsArrayBuffer = data.buffer
fromArrayBuffer(dataAsArrayBuffer)
.then((geoTIFF)=>{
geoTIFF.getImage()
.then((img)=> {
console.log(img.fileDirectory.GDAL_METADATA);//<==============
console.log(img.getWidth(), img.getHeight(), img.getSamplesPerPixel());
})
.catch((e)=>console.log("img.errorMessage:",e))
})
.catch((e)=> console.log("geoTIFF.errorMessage:",e))
})
.catch((e)=> console.log("data.errorMessage:",e))
});
答案1
得分: 2
以下是翻译好的部分:
例如 jsdom
const jsdom = require("jsdom");
const meta = `<GDALMetadata>
<Item name="STATISTICS_MAXIMUM" sample="0">21.122838228436</Item>
<Item name="STATISTICS_MEAN" sample="0">1.2389914218174</Item>
<Item name="STATISTICS_MINIMUM" sample="0">-4.6630500033646</Item>
<Item name="STATISTICS_STDDEV" sample="0">2.0382729681586</Item>
</GDALMetadata>`;
const dom = new jsdom.JSDOM(meta);
const stats = [...dom.window.document.querySelectorAll("Item")]
.map(item => ({[item.getAttribute('name')]:+item.textContent})); // 使用一元加号将字符串转换为数字
console.log(stats);
内置浏览器 DOMParser
const meta = `<GDALMetadata>
<Item name="STATISTICS_MAXIMUM" sample="0">21.122838228436</Item>
<Item name="STATISTICS_MEAN" sample="0">1.2389914218174</Item>
<Item name="STATISTICS_MINIMUM" sample="0">-4.6630500033646</Item>
<Item name="STATISTICS_STDDEV" sample="0">2.0382729681586</Item>
</GDALMetadata>`;
const parser = new DOMParser();
const doc1 = parser.parseFromString(meta, "application/xml");
const stats = [...doc1.querySelectorAll("Item")]
.map(item => ({[item.getAttribute('name')]:+item.textContent})); // 使用一元加号将字符串转换为数字
console.log(stats)
英文:
For example jsdom
const jsdom = require("jsdom");
const meta = `<GDALMetadata>
<Item name="STATISTICS_MAXIMUM" sample="0">21.122838228436</Item>
<Item name="STATISTICS_MEAN" sample="0">1.2389914218174</Item>
<Item name="STATISTICS_MINIMUM" sample="0">-4.6630500033646</Item>
<Item name="STATISTICS_STDDEV" sample="0">2.0382729681586</Item>
</GDALMetadata>`;
const dom = new jsdom.JSDOM(meta);
const stats = [...dom.window.document.querySelectorAll("Item")]
.map(item => ({[item.getAttribute('name')]:+item.textContent})); // convert the string to number using unary plus
console.log(stats);
Built-in Browser DOMParser
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const meta = `<GDALMetadata>
<Item name="STATISTICS_MAXIMUM" sample="0">21.122838228436</Item>
<Item name="STATISTICS_MEAN" sample="0">1.2389914218174</Item>
<Item name="STATISTICS_MINIMUM" sample="0">-4.6630500033646</Item>
<Item name="STATISTICS_STDDEV" sample="0">2.0382729681586</Item>
</GDALMetadata>`;
const parser = new DOMParser();
const doc1 = parser.parseFromString(meta, "application/xml");
const stats = [...doc1.querySelectorAll("Item")]
.map(item => ({[item.getAttribute('name')]:+item.textContent})); // convert the string to number using unary plus
console.log(stats)
<!-- end snippet -->
答案2
得分: 0
在Node.js服务器端使用cheerio等解析器解析HTML/XML是可行的。
尝试以下代码,它会加载你的字符串,查找所有的Item
元素,循环它们并获取文本值。然后,你可以根据需要构建结果对象:
const cheerio = require('cheerio');
const inputData = `<GDALMetadata>
<Item name="STATISTICS_MAXIMUM" sample="0">21.122838228436</Item>
<Item name="STATISTICS_MEAN" sample="0">1.2389914218174</Item>
<Item name="STATISTICS_MINIMUM" sample="0">-4.6630500033646</Item>
<Item name="STATISTICS_STDDEV" sample="0">2.0382729681586</Item>
</GDALMetadata>`;
const $ = cheerio.load(inputData, {
xmlMode: true
});
const result = {};
// 选择所有的 Item 元素,获取每个元素的文本值
$('Item').each(function(i, elm) {
console.log($(this).text());
// 构建对象 attributeName:value
result[$(this).attr('name')] = $(this).text();
});
console.log(result);
请注意,这段代码使用cheerio库加载XML数据,并遍历所有的Item
元素,将它们的属性名作为对象的键,文本值作为对象的值。最后,将结果打印出来。
英文:
Parsing HTML/XML on server side with Node.js. can be done with parsers such as cheerio
Try this, it loads your string, finds all Item
elements, loops them and gets text value. You can then construct results object according to your needs:
const cheerio = require('cheerio');
const inputData = `<GDALMetadata>
<Item name="STATISTICS_MAXIMUM" sample="0">21.122838228436</Item>
<Item name="STATISTICS_MEAN" sample="0">1.2389914218174</Item>
<Item name="STATISTICS_MINIMUM" sample="0">-4.6630500033646</Item>
<Item name="STATISTICS_STDDEV" sample="0">2.0382729681586</Item>
</GDALMetadata>`;
const $ = cheerio.load(inputData, {
xmlMode: true
});
const result = {};
// select all Item elements, get text value for each
$('Item').each(function(i, elm) {
console.log($(this).text());
// construct object attributeName:value
result[$(this).attr('name')] = $(this).text();
});
console.log(result);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论