NaturalIntelligence / fast-xml-parser

Validate XML, Parse XML and Build XML rapidly without C/C++ based libraries and no callback.
https://naturalintelligence.github.io/fast-xml-parser/
MIT License
2.45k stars 296 forks source link

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

Open ewlsh opened 1 year ago

ewlsh commented 1 year ago

Description

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

Potentially related: https://github.com/NaturalIntelligence/fast-xml-parser/issues/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?

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.

github-actions[bot] commented 1 year ago

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.

amitguptagwl commented 1 year ago

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

ewlsh commented 1 year ago

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;
};