XMLをNode.jsで触りたかったのでDOMParser
を使おうとしたら嵌まったので、サンプルを残す。
Node.jsはブラウザと違って DOMParser
がないので、tsconfig
でdom
をライブラリに加えてトランスパイルできたとしても、実行しようとするとエラーになる。
var parser = new DOMParser();
^
ReferenceError: DOMParser is not defined
そこで使うのが、「Trying to use the DOMParser with node js」で紹介されているjsdom
というモジュール。ブラウザと比べると使い勝手が若干違って、JSDOM
をインスタンス化してからDOMParser
をインスタンス化しないといけない。
import { readFileSync } from 'fs';
import { JSDOM } from 'jsdom';
const xml = readFileSync( './sample.xml', { encoding: 'utf-8' } );
const jsdom = new JSDOM();
const parser = new jsdom.window.DOMParser();
const dom = parser.parseFromString( xml, 'application/xhtml+xml' );
const addressList = dom.querySelectorAll( 'Address' );
const res: string[] = [];
for( let i=0; i<addressList.length; i++ ) {
res.push( addressList.item(i).getAttribute( 'Type' ) );
}
console.log( res );
querySelectorAll
の戻り値はNodeListOf<T>
で、通常はインデックスでループする。tsconfig
でdownlevelIteration
を有効化して、DOM.Iterable
ライブラリを使用すると、for of
でループできるようになる。
{
"compilerOptions": {
"downlevelIteration": true,
"lib": [
"dom",
"DOM.Iterable"
]
}
}
DOM.Iterable
を有効化して for of
ループに書き換えたら以下のようになる。
import { readFileSync } from 'fs';
import { JSDOM } from 'jsdom';
const xml = readFileSync( './sample.xml', { encoding: 'utf-8' } );
const jsdom = new JSDOM();
const parser = new jsdom.window.DOMParser();
const dom = parser.parseFromString( xml, 'application/xhtml+xml' );
const addressList = dom.querySelectorAll( 'Address' );
const res: string[] = [];
for( let address of addressList ) {
res.push( address.getAttribute('Type') );
}
console.log( res );