Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Document, Element, parseXml } from 'libxmljs';
- import { VError } from 'verror';
- export class XMLReader {
- private document: Document;
- private namespaces: Record<string, string>;
- constructor(inputXml: string) {
- try {
- this.document = parseXml(inputXml);
- this.namespaces = {};
- // Bad typings? No .namespaces()
- for (const ns of (this.document as any).namespaces()) {
- let name = ns.prefix();
- if (name === null) {
- name = '_';
- }
- this.namespaces[name] = ns.href();
- }
- } catch (cause) {
- throw new VError(
- { cause, name: 'ParseError', info: { inputXml } },
- 'Failed to parse XML',
- );
- }
- }
- public get(xpath: string): string {
- const element: Element | null = this.document.get(xpath, this.namespaces);
- if (element === null || element === undefined) {
- throw new Error(`XPath element "${xpath}" was not found`);
- }
- return element.text();
- }
- }
- const input: string = `<?xml version="1.0" encoding="UTF-8"?>
- <root xmlns="default:namespace" xmlns:opt="inner:namespace">
- <outer><opt:inner>value</opt:inner></outer>
- </root>
- `;
- const xml = new XMLReader(input);
- // Need to specify the default namespace with _.
- console.log(xml.get('/_:root/_:outer/opt:inner'));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement