Overview

To parse XML strings and extract information from them in Node.js, I recommend using the xmldom library. This allows you to work with XML in a way similar to how you manipulate the DOM in a browser. Below is how to set up a function to parse XML and extract elements, focusing on “PAGE” tags, using xmldom.

  1. Install the xmldom library: First, install xmldom, which is needed to parse XML strings.
npm install xmldom
  1. Use xmldom to parse XML and extract the required elements.
const { DOMParser } = require('xmldom');

const xmlString = "...";

// DOMParserを使用してXML文字列を解析
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xmlString, 'text/xml');

// 全てのPAGE要素を取得
const pages = xmlDoc.getElementsByTagName('PAGE');

// 発見されたPAGE要素の数をログに記録(例)
console.log('PAGE要素の数:', pages.length);

In this example, the basic function logs the XML string, parses it into a document, iterates over each “PAGE” element, and logs its attributes and content. The processing within the loop can be customized based on specific requirements, such as extracting particular details from each page.