Skip to content
A

Extract fields with a custom schema

The parseCPT, parseBHRGT, and parseBHRG methods return the full data model for each file type. When you need only a few fields, you can define a schema and call parseCustom. A schema maps output keys to an XPath expression and an optional resolver that converts the matched node into a typed value.

import { BROParser, XMLAdapter, resolvers } from "@bedrock-engineer/bro-xml-parser/node";
const parser = new BROParser(new XMLAdapter());
const schema = {
id: { xpath: "brocom:broId" },
depth: {
xpath: ".//cptcommon:finalDepth",
resolver: resolvers.parseFloat,
},
location: {
xpath: "./dscpt:deliveredLocation/cptcommon:location",
resolver: resolvers.parseGMLLocation,
},
};
const result = parser.parseCustom(xmlText, schema, "CPT");
// { id: "CPT000000099543", depth: 25.5, location: { x: 155000, y: 463000, epsg: "28992" } }

The third argument is the data type. Passing it lets the parser validate the schema version of the document before extracting.

A resolver turns a matched XML node into a JavaScript value. Without one, a field returns the raw text content. The resolvers namespace holds the built-in converters, including parseFloat, parseInt, parseBoolean, parseDate, and parseGMLLocation. You can also pass your own function with the same signature.

See the resolvers reference for the full list.

For common cases, the presets namespace exports ready-made schemas, for example ID-only, location-only, and metadata-only variants for each file type:

import { BROParser, XMLAdapter, presets } from "@bedrock-engineer/bro-xml-parser/node";
const parser = new BROParser(new XMLAdapter());
const location = parser.parseCustom(xmlText, presets.CPT_LOCATION_ONLY, "CPT");