NaturalIntelligence / fast-xml-parser

Validate XML, Parse XML and Build XML rapidly without C/C++ based libraries and no callback.

Home Page:https://naturalintelligence.github.io/fast-xml-parser/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature Request: Allow including node metadata (startIndex) in output

ewlsh opened this issue · comments

  • Are you running the latest version?
  • Have you included sample input, output, error, and expected output?
  • Have you checked if you are using correct configuration?
  • Did you try online tool?

Description

Feature request to have a way to include metadata such as startIndex in the parser output

Potentially related: #345

Background

I'm working on a project that uses fast-xml-parser to parse XML files, it needs to report where a certain string was found in a given file. To support this I'm currently monkeypatching XmlNode: https://github.com/ewlsh/tree-gettext/blob/main/src/languages/xml.ts#L11

Would you like to work on this issue?

  • Yes
  • No

I am open to contributing but am submitting this feature request first to gather input.

Bookmark this repository for further updates. Visit SoloThought to know about recent features.

We're glad you find this project helpful. We'll try to address this issue ASAP. You can vist https://solothought.com to know recent features. Don't forget to star this repo.

Do you mean tag index line number, or line number with col number? There is a plan to add tag index in v5.

Do you mean tag index line number, or line number with col number? There is a plan to add tag index in v5.

@amitguptagwl I'm looking for startIndex (and maybe endIndex), I can calculate lines and columns off of that value.

Currently I'm doing something like this against ^4.0.0-beta.2

/// Temporarily construct our own Xml parser from fast-xml-parser which preserves start indices
import { parseToOrderedJsObj } from 'fast-xml-parser/src/xmlparser/OrderedObjParser';
import { buildOptions } from 'fast-xml-parser/src/xmlparser/OptionsBuilder';

import XmlNode from 'fast-xml-parser/src/xmlparser/xmlNode';
import { ParseResult } from '../parse';

const addChild = XmlNode.prototype.addChild;
XmlNode.prototype.addChild = function _addChild(node) {
  addChild.call(this, node);
  this.child[this.child.length - 1].$startIndex = node.startIndex ?? null;
};