XML 글자를 행으로 읽어 넣기Read XML text into rows
importXml(text, options)는 뿌리 아래 요소 하나를 행 하나로 읽어요. 칸은 자식 요소(<id>1</id>)나 속성(<person id="3"/>)이고 & 같은 기본 엔티티, CDATA, 숫자 엔티티를 풀어요. DOCTYPE와 사용자 정의 엔티티, 깊이 64 초과, 크기 상한 초과는 parse-failed로 거절해요. rowTag·path로 행이 있는 곳을 가리켜요.importXml(text, options) reads each element under the root as a row. Cells are child elements (<id>1</id>) or attributes (<person id="3"/>), and the predefined entities such as &, CDATA and numeric entities are decoded. DOCTYPE, custom entities, nesting beyond 64 and inputs over the size limit are refused with parse-failed. rowTag and path point to where the rows are.
코드Code
탭을 골라 코드를 복사하세요. 위 화면은 이 코드를 그대로 실행한 결과예요.Pick a tab and copy the code. The preview above runs exactly this code.
이 화면은 새 모양 옵션을 켠 모양이에요: density: 'spacious', iconStyle: 'soft', motion: true (코드에 없으면 기본 모양으로 보여요)This preview has the new-look options on: density: 'spacious', iconStyle: 'soft', motion: true (without them in your code you get the default look)
이 예제는 추가 패키지가 필요해요. 내 컴퓨터에서 돌리려면 npm i maxgrid @maxgrid/export로 설치하세요.
버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<div id="app">
<div class="toolbar">
<button type="button" id="elements">요소 형식</button>
<button type="button" id="attributes">속성 형식</button>
<button type="button" id="doctype">DOCTYPE 거절</button>
</div>
<div id="grid" style="height: 300px"></div>
</div>
<script type="module" src="./main.js"></script>import { createGrid } from 'maxgrid';
import { actions, options, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api, console.log);
document.getElementById('elements').addEventListener('click', () => actions.elements(api));
document.getElementById('attributes').addEventListener('click', () => actions.attributes(api));
document.getElementById('doctype').addEventListener('click', () => actions.doctype(api));import { withExport } from '@maxgrid/export';
const rows = [{ id: 0, name: '(예전 행)', dept: 'dev', joined: null, active: false }];
export const options = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 70 },
{ id: 'name', title: '이름', width: 150 },
{
id: 'dept',
title: '부서',
kind: 'select',
width: 100,
editorOptions: [
{ value: 'dev', label: '개발' },
{ value: 'ops', label: '운영' },
],
optionLabel: (v) => (v === 'dev' ? '개발' : '운영'),
},
{ id: 'joined', title: '입사일', kind: 'date', width: 110 },
{ id: 'active', title: '재직', kind: 'boolean', width: 70 },
],
rows,
rowKey: 'id',
};
const ELEMENTS = `<?xml version="1.0" encoding="UTF-8"?>
<rows>
<row><id>1</id><name>김하늘 & 팀</name><dept>운영</dept><joined>2024-03-02</joined><active>true</active></row>
<row><id>2</id><name><![CDATA[이도윤 <리더>]]></name><dept>개발</dept><joined nil="true"/><active>false</active></row>
</rows>`;
const ATTRIBUTES = '<people><person id="3" name="박서연" dept="개발" joined="2025.1.15" active="yes"/><person id="4" name="최지우" dept="운영"/></people>';
const DOCTYPE = '<!DOCTYPE rows [<!ENTITY secret SYSTEM "file:///etc/passwd">]><rows><row><name>&secret;</name></row></rows>';
let say = () => { };
let ex;
export function setup(api, log) {
say = log;
ex = withExport(api);
log({
ko: 'importXml(text)는 요소 형식과 속성 형식을 모두 읽어요. DOM 없이 직접 읽으므로 작업자에서도 돌아요.',
en: 'importXml(text) reads both the element form and the attribute form. It parses without a DOM, so it also runs in a worker.',
});
}
export const actions = {
elements: (api) => {
const result = ex?.importXml(ELEMENTS);
if (!result)
return;
api.setRows(result.rows);
say({
ko: `요소 형식 ${result.rows.length}행. &·CDATA가 풀리고 nil은 null이에요.`,
en: `${result.rows.length} rows from the element form. & and CDATA are decoded and nil becomes null.`,
});
},
attributes: (api) => {
const result = ex?.importXml(ATTRIBUTES, { rowTag: 'person' });
if (!result)
return;
api.setRows(result.rows);
say({
ko: `속성 형식 ${result.rows.length}행(rowTag: 'person').`,
en: `${result.rows.length} rows from the attribute form (rowTag: 'person').`,
});
},
doctype: () => {
try {
ex?.importXml(DOCTYPE);
}
catch (error) {
const e = error;
say({ ko: `거절했어요(${e.code}): ${e.message}`, en: `Refused (${e.code}): ${e.message}` });
}
},
}; 이 HTML은 추가 번들(위 script 태그)도 함께 불러오고 전역 변수로 써요. 별도 설치는 필요 없어요.
<!doctype html>
<html lang="ko">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>XML 글자를 행으로 읽어 넣기</title>
</head>
<body>
<div class="toolbar">
<button type="button" id="elements">요소 형식</button>
<button type="button" id="attributes">속성 형식</button>
<button type="button" id="doctype">DOCTYPE 거절</button>
</div>
<div id="grid" style="height: 300px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.export.min.js"></script>
<script>
const { withExport } = MaxGridExport;
const rows = [{ id: 0, name: '(예전 행)', dept: 'dev', joined: null, active: false }];
const options = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 70 },
{ id: 'name', title: '이름', width: 150 },
{
id: 'dept',
title: '부서',
kind: 'select',
width: 100,
editorOptions: [
{ value: 'dev', label: '개발' },
{ value: 'ops', label: '운영' },
],
optionLabel: (v) => (v === 'dev' ? '개발' : '운영'),
},
{ id: 'joined', title: '입사일', kind: 'date', width: 110 },
{ id: 'active', title: '재직', kind: 'boolean', width: 70 },
],
rows,
rowKey: 'id',
};
const ELEMENTS = `<?xml version="1.0" encoding="UTF-8"?>
<rows>
<row><id>1</id><name>김하늘 & 팀</name><dept>운영</dept><joined>2024-03-02</joined><active>true</active></row>
<row><id>2</id><name><![CDATA[이도윤 <리더>]]></name><dept>개발</dept><joined nil="true"/><active>false</active></row>
</rows>`;
const ATTRIBUTES = '<people><person id="3" name="박서연" dept="개발" joined="2025.1.15" active="yes"/><person id="4" name="최지우" dept="운영"/></people>';
const DOCTYPE = '<!DOCTYPE rows [<!ENTITY secret SYSTEM "file:///etc/passwd">]><rows><row><name>&secret;</name></row></rows>';
let say = () => { };
let ex;
function setup(api, log) {
say = log;
ex = withExport(api);
log({
ko: 'importXml(text)는 요소 형식과 속성 형식을 모두 읽어요. DOM 없이 직접 읽으므로 작업자에서도 돌아요.',
en: 'importXml(text) reads both the element form and the attribute form. It parses without a DOM, so it also runs in a worker.',
});
}
const actions = {
elements: (api) => {
const result = ex?.importXml(ELEMENTS);
if (!result)
return;
api.setRows(result.rows);
say({
ko: `요소 형식 ${result.rows.length}행. &·CDATA가 풀리고 nil은 null이에요.`,
en: `${result.rows.length} rows from the element form. & and CDATA are decoded and nil becomes null.`,
});
},
attributes: (api) => {
const result = ex?.importXml(ATTRIBUTES, { rowTag: 'person' });
if (!result)
return;
api.setRows(result.rows);
say({
ko: `속성 형식 ${result.rows.length}행(rowTag: 'person').`,
en: `${result.rows.length} rows from the attribute form (rowTag: 'person').`,
});
},
doctype: () => {
try {
ex?.importXml(DOCTYPE);
}
catch (error) {
const e = error;
say({ ko: `거절했어요(${e.code}): ${e.message}`, en: `Refused (${e.code}): ${e.message}` });
}
},
};
options.ariaLabel = "XML 글자를 행으로 읽어 넣기";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
setup(api, (message) => {
const item = document.createElement('li');
item.textContent =
typeof message === 'string' ? message : navigator.language.startsWith('ko') ? message.ko : message.en;
document.getElementById('log').prepend(item);
});
document.getElementById('elements').addEventListener('click', () => actions.elements(api));
document.getElementById('attributes').addEventListener('click', () => actions.attributes(api));
document.getElementById('doctype').addEventListener('click', () => actions.doctype(api));
</script>
</body>
</html> 이 예제는 추가 패키지가 필요해요. 내 컴퓨터에서 돌리려면 npm i @maxgrid/react @maxgrid/export로 설치하세요.
버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { useEffect } from 'react';
import { Grid, useGridApi } from '@maxgrid/react';
import { actions, options, setup, type Row } from './example';
export function ExampleGrid() {
const [api, gridRef] = useGridApi<Row>();
useEffect(() => (api ? setup(api, console.log) : undefined), [api]);
return (
<>
<div className="toolbar">
<button
type="button"
disabled={!api}
onClick={() => api && actions.elements(api)}
>요소 형식</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.attributes(api)}
>속성 형식</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.doctype(api)}
>DOCTYPE 거절</button>
</div>
<Grid style={{ height: 300 }} ref={gridRef} options={options} />
</>
);
}import { withExport, type ExportableGridApi } from '@maxgrid/export';
import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
dept: string;
joined: string | null;
active: boolean;
}
const rows: Row[] = [{ id: 0, name: '(예전 행)', dept: 'dev', joined: null, active: false }];
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 70 },
{ id: 'name', title: '이름', width: 150 },
{
id: 'dept',
title: '부서',
kind: 'select',
width: 100,
editorOptions: [
{ value: 'dev', label: '개발' },
{ value: 'ops', label: '운영' },
],
optionLabel: (v) => (v === 'dev' ? '개발' : '운영'),
},
{ id: 'joined', title: '입사일', kind: 'date', width: 110 },
{ id: 'active', title: '재직', kind: 'boolean', width: 70 },
],
rows,
rowKey: 'id',
};
const ELEMENTS = `<?xml version="1.0" encoding="UTF-8"?>
<rows>
<row><id>1</id><name>김하늘 & 팀</name><dept>운영</dept><joined>2024-03-02</joined><active>true</active></row>
<row><id>2</id><name><![CDATA[이도윤 <리더>]]></name><dept>개발</dept><joined nil="true"/><active>false</active></row>
</rows>`;
const ATTRIBUTES =
'<people><person id="3" name="박서연" dept="개발" joined="2025.1.15" active="yes"/><person id="4" name="최지우" dept="운영"/></people>';
const DOCTYPE =
'<!DOCTYPE rows [<!ENTITY secret SYSTEM "file:///etc/passwd">]><rows><row><name>&secret;</name></row></rows>';
let say: (message: LogText) => void = () => {};
let ex: ExportableGridApi<Row> | undefined;
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
ex = withExport(api);
log({
ko: 'importXml(text)는 요소 형식과 속성 형식을 모두 읽어요. DOM 없이 직접 읽으므로 작업자에서도 돌아요.',
en: 'importXml(text) reads both the element form and the attribute form. It parses without a DOM, so it also runs in a worker.',
});
}
export const actions = {
elements: (api: GridApi<Row>) => {
const result = ex?.importXml(ELEMENTS);
if (!result) return;
api.setRows(result.rows);
say({
ko: `요소 형식 ${result.rows.length}행. &·CDATA가 풀리고 nil은 null이에요.`,
en: `${result.rows.length} rows from the element form. & and CDATA are decoded and nil becomes null.`,
});
},
attributes: (api: GridApi<Row>) => {
const result = ex?.importXml(ATTRIBUTES, { rowTag: 'person' });
if (!result) return;
api.setRows(result.rows);
say({
ko: `속성 형식 ${result.rows.length}행(rowTag: 'person').`,
en: `${result.rows.length} rows from the attribute form (rowTag: 'person').`,
});
},
doctype: () => {
try {
ex?.importXml(DOCTYPE);
} catch (error) {
const e = error as Error & { code?: string };
say({ ko: `거절했어요(${e.code}): ${e.message}`, en: `Refused (${e.code}): ${e.message}` });
}
},
}; 이 예제는 추가 패키지가 필요해요. 내 컴퓨터에서 돌리려면 npm i @maxgrid/vue @maxgrid/export로 설치하세요.
버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<script setup lang="ts">
import { onBeforeUnmount, shallowRef } from 'vue';
import { Grid, type GridApi } from '@maxgrid/vue';
import { actions, options, setup, type Row } from './example';
const api = shallowRef<GridApi<Row> | null>(null);
let stop: (() => void) | undefined;
function onReady(event: { api: GridApi<Row> }) {
api.value = event.api;
stop = setup(event.api, console.log);
}
onBeforeUnmount(() => stop?.());
</script>
<template>
<div class="toolbar">
<button
type="button"
:disabled="!api"
@click="api && actions.elements(api)"
>요소 형식</button>
<button
type="button"
:disabled="!api"
@click="api && actions.attributes(api)"
>속성 형식</button>
<button type="button" :disabled="!api" @click="api && actions.doctype(api)">DOCTYPE 거절</button>
</div>
<Grid style="height: 300px" :options="options" @ready="onReady" />
</template>import { withExport, type ExportableGridApi } from '@maxgrid/export';
import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
dept: string;
joined: string | null;
active: boolean;
}
const rows: Row[] = [{ id: 0, name: '(예전 행)', dept: 'dev', joined: null, active: false }];
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 70 },
{ id: 'name', title: '이름', width: 150 },
{
id: 'dept',
title: '부서',
kind: 'select',
width: 100,
editorOptions: [
{ value: 'dev', label: '개발' },
{ value: 'ops', label: '운영' },
],
optionLabel: (v) => (v === 'dev' ? '개발' : '운영'),
},
{ id: 'joined', title: '입사일', kind: 'date', width: 110 },
{ id: 'active', title: '재직', kind: 'boolean', width: 70 },
],
rows,
rowKey: 'id',
};
const ELEMENTS = `<?xml version="1.0" encoding="UTF-8"?>
<rows>
<row><id>1</id><name>김하늘 & 팀</name><dept>운영</dept><joined>2024-03-02</joined><active>true</active></row>
<row><id>2</id><name><![CDATA[이도윤 <리더>]]></name><dept>개발</dept><joined nil="true"/><active>false</active></row>
</rows>`;
const ATTRIBUTES =
'<people><person id="3" name="박서연" dept="개발" joined="2025.1.15" active="yes"/><person id="4" name="최지우" dept="운영"/></people>';
const DOCTYPE =
'<!DOCTYPE rows [<!ENTITY secret SYSTEM "file:///etc/passwd">]><rows><row><name>&secret;</name></row></rows>';
let say: (message: LogText) => void = () => {};
let ex: ExportableGridApi<Row> | undefined;
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
ex = withExport(api);
log({
ko: 'importXml(text)는 요소 형식과 속성 형식을 모두 읽어요. DOM 없이 직접 읽으므로 작업자에서도 돌아요.',
en: 'importXml(text) reads both the element form and the attribute form. It parses without a DOM, so it also runs in a worker.',
});
}
export const actions = {
elements: (api: GridApi<Row>) => {
const result = ex?.importXml(ELEMENTS);
if (!result) return;
api.setRows(result.rows);
say({
ko: `요소 형식 ${result.rows.length}행. &·CDATA가 풀리고 nil은 null이에요.`,
en: `${result.rows.length} rows from the element form. & and CDATA are decoded and nil becomes null.`,
});
},
attributes: (api: GridApi<Row>) => {
const result = ex?.importXml(ATTRIBUTES, { rowTag: 'person' });
if (!result) return;
api.setRows(result.rows);
say({
ko: `속성 형식 ${result.rows.length}행(rowTag: 'person').`,
en: `${result.rows.length} rows from the attribute form (rowTag: 'person').`,
});
},
doctype: () => {
try {
ex?.importXml(DOCTYPE);
} catch (error) {
const e = error as Error & { code?: string };
say({ ko: `거절했어요(${e.code}): ${e.message}`, en: `Refused (${e.code}): ${e.message}` });
}
},
}; 이 예제는 추가 패키지가 필요해요. 내 컴퓨터에서 돌리려면 npm i @maxgrid/angular @maxgrid/export로 설치하세요.
버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { Component, OnDestroy } from '@angular/core';
import { GridComponent, isGridEvent, type GridApi, type GridEventMessage } from '@maxgrid/angular';
import { actions, options, setup, type Row } from './example';
@Component({
selector: 'app-example-grid',
standalone: true,
imports: [GridComponent],
template: `
<div class="toolbar">
<button
type="button"
[disabled]="!api"
(click)="api && actions.elements(api)"
>요소 형식</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.attributes(api)"
>속성 형식</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.doctype(api)"
>DOCTYPE 거절</button>
</div>
<mx-grid style="height: 300px" [options]="options" [events]="relayed" (gridEvent)="onEvent($event)" />
`,
})
export class ExampleGridComponent implements OnDestroy {
readonly options = options;
readonly actions = actions;
readonly relayed = ['ready'] as const;
api: GridApi<Row> | null = null;
private stop: (() => void) | null = null;
onEvent(event: GridEventMessage<Row>): void {
if (isGridEvent(event, 'ready')) {
this.api = event.payload.api;
this.stop = setup(this.api, console.log);
}
}
ngOnDestroy(): void {
this.stop?.();
}
}import { withExport, type ExportableGridApi } from '@maxgrid/export';
import type { GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
dept: string;
joined: string | null;
active: boolean;
}
const rows: Row[] = [{ id: 0, name: '(예전 행)', dept: 'dev', joined: null, active: false }];
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 70 },
{ id: 'name', title: '이름', width: 150 },
{
id: 'dept',
title: '부서',
kind: 'select',
width: 100,
editorOptions: [
{ value: 'dev', label: '개발' },
{ value: 'ops', label: '운영' },
],
optionLabel: (v) => (v === 'dev' ? '개발' : '운영'),
},
{ id: 'joined', title: '입사일', kind: 'date', width: 110 },
{ id: 'active', title: '재직', kind: 'boolean', width: 70 },
],
rows,
rowKey: 'id',
};
const ELEMENTS = `<?xml version="1.0" encoding="UTF-8"?>
<rows>
<row><id>1</id><name>김하늘 & 팀</name><dept>운영</dept><joined>2024-03-02</joined><active>true</active></row>
<row><id>2</id><name><![CDATA[이도윤 <리더>]]></name><dept>개발</dept><joined nil="true"/><active>false</active></row>
</rows>`;
const ATTRIBUTES =
'<people><person id="3" name="박서연" dept="개발" joined="2025.1.15" active="yes"/><person id="4" name="최지우" dept="운영"/></people>';
const DOCTYPE =
'<!DOCTYPE rows [<!ENTITY secret SYSTEM "file:///etc/passwd">]><rows><row><name>&secret;</name></row></rows>';
let say: (message: LogText) => void = () => {};
let ex: ExportableGridApi<Row> | undefined;
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
ex = withExport(api);
log({
ko: 'importXml(text)는 요소 형식과 속성 형식을 모두 읽어요. DOM 없이 직접 읽으므로 작업자에서도 돌아요.',
en: 'importXml(text) reads both the element form and the attribute form. It parses without a DOM, so it also runs in a worker.',
});
}
export const actions = {
elements: (api: GridApi<Row>) => {
const result = ex?.importXml(ELEMENTS);
if (!result) return;
api.setRows(result.rows);
say({
ko: `요소 형식 ${result.rows.length}행. &·CDATA가 풀리고 nil은 null이에요.`,
en: `${result.rows.length} rows from the element form. & and CDATA are decoded and nil becomes null.`,
});
},
attributes: (api: GridApi<Row>) => {
const result = ex?.importXml(ATTRIBUTES, { rowTag: 'person' });
if (!result) return;
api.setRows(result.rows);
say({
ko: `속성 형식 ${result.rows.length}행(rowTag: 'person').`,
en: `${result.rows.length} rows from the attribute form (rowTag: 'person').`,
});
},
doctype: () => {
try {
ex?.importXml(DOCTYPE);
} catch (error) {
const e = error as Error & { code?: string };
say({ ko: `거절했어요(${e.code}): ${e.message}`, en: `Refused (${e.code}): ${e.message}` });
}
},
}; This example needs an extra package. To run it locally, install with npm i maxgrid @maxgrid/export.
When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<div id="app">
<div class="toolbar">
<button type="button" id="elements">Element form</button>
<button type="button" id="attributes">Attribute form</button>
<button type="button" id="doctype">Refuse DOCTYPE</button>
</div>
<div id="grid" style="height: 300px"></div>
</div>
<script type="module" src="./main.js"></script>import { createGrid } from 'maxgrid';
import { actions, options, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api, console.log);
document.getElementById('elements').addEventListener('click', () => actions.elements(api));
document.getElementById('attributes').addEventListener('click', () => actions.attributes(api));
document.getElementById('doctype').addEventListener('click', () => actions.doctype(api));import { withExport } from '@maxgrid/export';
const rows = [{ id: 0, name: '(old row)', dept: 'dev', joined: null, active: false }];
export const options = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 70 },
{ id: 'name', title: 'Name', width: 150 },
{
id: 'dept',
title: 'Department',
kind: 'select',
width: 120,
editorOptions: [
{ value: 'dev', label: 'Dev' },
{ value: 'ops', label: 'Ops' },
],
optionLabel: (v) => (v === 'dev' ? 'Dev' : 'Ops'),
},
{ id: 'joined', title: 'Joined', kind: 'date', width: 110 },
{ id: 'active', title: 'Active', kind: 'boolean', width: 80 },
],
rows,
rowKey: 'id',
};
const ELEMENTS = `<?xml version="1.0" encoding="UTF-8"?>
<rows>
<row><id>1</id><name>Carter & Co</name><dept>Ops</dept><joined>2024-03-02</joined><active>true</active></row>
<row><id>2</id><name><![CDATA[Emma Stone <Lead>]]></name><dept>Dev</dept><joined nil="true"/><active>false</active></row>
</rows>`;
const ATTRIBUTES = '<people><person id="3" name="Noah Reed" dept="Dev" joined="2025.1.15" active="yes"/><person id="4" name="Olivia Hayes" dept="Ops"/></people>';
const DOCTYPE = '<!DOCTYPE rows [<!ENTITY secret SYSTEM "file:///etc/passwd">]><rows><row><name>&secret;</name></row></rows>';
let say = () => { };
let ex;
export function setup(api, log) {
say = log;
ex = withExport(api);
log('importXml(text) reads both the element form and the attribute form. It parses without a DOM, so it also runs in a worker.');
}
export const actions = {
elements: (api) => {
const result = ex?.importXml(ELEMENTS);
if (!result)
return;
api.setRows(result.rows);
say(`${result.rows.length} rows from the element form. & and CDATA are decoded and nil becomes null.`);
},
attributes: (api) => {
const result = ex?.importXml(ATTRIBUTES, { rowTag: 'person' });
if (!result)
return;
api.setRows(result.rows);
say(`${result.rows.length} rows from the attribute form (rowTag: 'person').`);
},
doctype: () => {
try {
ex?.importXml(DOCTYPE);
}
catch (error) {
const e = error;
say(`Refused (${e.code}): ${e.message}`);
}
},
};This HTML also loads an extra bundle (the extra script tag above) and uses it as a global. No install is needed.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Read XML text into rows</title>
</head>
<body>
<div class="toolbar">
<button type="button" id="elements">Element form</button>
<button type="button" id="attributes">Attribute form</button>
<button type="button" id="doctype">Refuse DOCTYPE</button>
</div>
<div id="grid" style="height: 300px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.export.min.js"></script>
<script>
const { withExport } = MaxGridExport;
const rows = [{ id: 0, name: '(old row)', dept: 'dev', joined: null, active: false }];
const options = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 70 },
{ id: 'name', title: 'Name', width: 150 },
{
id: 'dept',
title: 'Department',
kind: 'select',
width: 120,
editorOptions: [
{ value: 'dev', label: 'Dev' },
{ value: 'ops', label: 'Ops' },
],
optionLabel: (v) => (v === 'dev' ? 'Dev' : 'Ops'),
},
{ id: 'joined', title: 'Joined', kind: 'date', width: 110 },
{ id: 'active', title: 'Active', kind: 'boolean', width: 80 },
],
rows,
rowKey: 'id',
};
const ELEMENTS = `<?xml version="1.0" encoding="UTF-8"?>
<rows>
<row><id>1</id><name>Carter & Co</name><dept>Ops</dept><joined>2024-03-02</joined><active>true</active></row>
<row><id>2</id><name><![CDATA[Emma Stone <Lead>]]></name><dept>Dev</dept><joined nil="true"/><active>false</active></row>
</rows>`;
const ATTRIBUTES = '<people><person id="3" name="Noah Reed" dept="Dev" joined="2025.1.15" active="yes"/><person id="4" name="Olivia Hayes" dept="Ops"/></people>';
const DOCTYPE = '<!DOCTYPE rows [<!ENTITY secret SYSTEM "file:///etc/passwd">]><rows><row><name>&secret;</name></row></rows>';
let say = () => { };
let ex;
function setup(api, log) {
say = log;
ex = withExport(api);
log('importXml(text) reads both the element form and the attribute form. It parses without a DOM, so it also runs in a worker.');
}
const actions = {
elements: (api) => {
const result = ex?.importXml(ELEMENTS);
if (!result)
return;
api.setRows(result.rows);
say(`${result.rows.length} rows from the element form. & and CDATA are decoded and nil becomes null.`);
},
attributes: (api) => {
const result = ex?.importXml(ATTRIBUTES, { rowTag: 'person' });
if (!result)
return;
api.setRows(result.rows);
say(`${result.rows.length} rows from the attribute form (rowTag: 'person').`);
},
doctype: () => {
try {
ex?.importXml(DOCTYPE);
}
catch (error) {
const e = error;
say(`Refused (${e.code}): ${e.message}`);
}
},
};
options.ariaLabel = "Read XML text into rows";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
setup(api, (message) => {
const item = document.createElement('li');
item.textContent =
typeof message === 'string' ? message : navigator.language.startsWith('ko') ? message.ko : message.en;
document.getElementById('log').prepend(item);
});
document.getElementById('elements').addEventListener('click', () => actions.elements(api));
document.getElementById('attributes').addEventListener('click', () => actions.attributes(api));
document.getElementById('doctype').addEventListener('click', () => actions.doctype(api));
</script>
</body>
</html> This example needs an extra package. To run it locally, install with npm i @maxgrid/react @maxgrid/export.
When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
import { useEffect } from 'react';
import { Grid, useGridApi } from '@maxgrid/react';
import { actions, options, setup, type Row } from './example';
export function ExampleGrid() {
const [api, gridRef] = useGridApi<Row>();
useEffect(() => (api ? setup(api, console.log) : undefined), [api]);
return (
<>
<div className="toolbar">
<button
type="button"
disabled={!api}
onClick={() => api && actions.elements(api)}
>Element form</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.attributes(api)}
>Attribute form</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.doctype(api)}
>Refuse DOCTYPE</button>
</div>
<Grid style={{ height: 300 }} ref={gridRef} options={options} />
</>
);
}import { withExport, type ExportableGridApi } from '@maxgrid/export';
import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string;
export interface Row {
id: number;
name: string;
dept: string;
joined: string | null;
active: boolean;
}
const rows: Row[] = [{ id: 0, name: '(old row)', dept: 'dev', joined: null, active: false }];
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 70 },
{ id: 'name', title: 'Name', width: 150 },
{
id: 'dept',
title: 'Department',
kind: 'select',
width: 120,
editorOptions: [
{ value: 'dev', label: 'Dev' },
{ value: 'ops', label: 'Ops' },
],
optionLabel: (v) => (v === 'dev' ? 'Dev' : 'Ops'),
},
{ id: 'joined', title: 'Joined', kind: 'date', width: 110 },
{ id: 'active', title: 'Active', kind: 'boolean', width: 80 },
],
rows,
rowKey: 'id',
};
const ELEMENTS = `<?xml version="1.0" encoding="UTF-8"?>
<rows>
<row><id>1</id><name>Carter & Co</name><dept>Ops</dept><joined>2024-03-02</joined><active>true</active></row>
<row><id>2</id><name><![CDATA[Emma Stone <Lead>]]></name><dept>Dev</dept><joined nil="true"/><active>false</active></row>
</rows>`;
const ATTRIBUTES =
'<people><person id="3" name="Noah Reed" dept="Dev" joined="2025.1.15" active="yes"/><person id="4" name="Olivia Hayes" dept="Ops"/></people>';
const DOCTYPE =
'<!DOCTYPE rows [<!ENTITY secret SYSTEM "file:///etc/passwd">]><rows><row><name>&secret;</name></row></rows>';
let say: (message: LogText) => void = () => {};
let ex: ExportableGridApi<Row> | undefined;
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
ex = withExport(api);
log(
'importXml(text) reads both the element form and the attribute form. It parses without a DOM, so it also runs in a worker.',
);
}
export const actions = {
elements: (api: GridApi<Row>) => {
const result = ex?.importXml(ELEMENTS);
if (!result) return;
api.setRows(result.rows);
say(
`${result.rows.length} rows from the element form. & and CDATA are decoded and nil becomes null.`,
);
},
attributes: (api: GridApi<Row>) => {
const result = ex?.importXml(ATTRIBUTES, { rowTag: 'person' });
if (!result) return;
api.setRows(result.rows);
say(`${result.rows.length} rows from the attribute form (rowTag: 'person').`);
},
doctype: () => {
try {
ex?.importXml(DOCTYPE);
} catch (error) {
const e = error as Error & { code?: string };
say(`Refused (${e.code}): ${e.message}`);
}
},
}; This example needs an extra package. To run it locally, install with npm i @maxgrid/vue @maxgrid/export.
When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<script setup lang="ts">
import { onBeforeUnmount, shallowRef } from 'vue';
import { Grid, type GridApi } from '@maxgrid/vue';
import { actions, options, setup, type Row } from './example';
const api = shallowRef<GridApi<Row> | null>(null);
let stop: (() => void) | undefined;
function onReady(event: { api: GridApi<Row> }) {
api.value = event.api;
stop = setup(event.api, console.log);
}
onBeforeUnmount(() => stop?.());
</script>
<template>
<div class="toolbar">
<button
type="button"
:disabled="!api"
@click="api && actions.elements(api)"
>Element form</button>
<button
type="button"
:disabled="!api"
@click="api && actions.attributes(api)"
>Attribute form</button>
<button type="button" :disabled="!api" @click="api && actions.doctype(api)">Refuse DOCTYPE</button>
</div>
<Grid style="height: 300px" :options="options" @ready="onReady" />
</template>import { withExport, type ExportableGridApi } from '@maxgrid/export';
import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string;
export interface Row {
id: number;
name: string;
dept: string;
joined: string | null;
active: boolean;
}
const rows: Row[] = [{ id: 0, name: '(old row)', dept: 'dev', joined: null, active: false }];
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 70 },
{ id: 'name', title: 'Name', width: 150 },
{
id: 'dept',
title: 'Department',
kind: 'select',
width: 120,
editorOptions: [
{ value: 'dev', label: 'Dev' },
{ value: 'ops', label: 'Ops' },
],
optionLabel: (v) => (v === 'dev' ? 'Dev' : 'Ops'),
},
{ id: 'joined', title: 'Joined', kind: 'date', width: 110 },
{ id: 'active', title: 'Active', kind: 'boolean', width: 80 },
],
rows,
rowKey: 'id',
};
const ELEMENTS = `<?xml version="1.0" encoding="UTF-8"?>
<rows>
<row><id>1</id><name>Carter & Co</name><dept>Ops</dept><joined>2024-03-02</joined><active>true</active></row>
<row><id>2</id><name><![CDATA[Emma Stone <Lead>]]></name><dept>Dev</dept><joined nil="true"/><active>false</active></row>
</rows>`;
const ATTRIBUTES =
'<people><person id="3" name="Noah Reed" dept="Dev" joined="2025.1.15" active="yes"/><person id="4" name="Olivia Hayes" dept="Ops"/></people>';
const DOCTYPE =
'<!DOCTYPE rows [<!ENTITY secret SYSTEM "file:///etc/passwd">]><rows><row><name>&secret;</name></row></rows>';
let say: (message: LogText) => void = () => {};
let ex: ExportableGridApi<Row> | undefined;
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
ex = withExport(api);
log(
'importXml(text) reads both the element form and the attribute form. It parses without a DOM, so it also runs in a worker.',
);
}
export const actions = {
elements: (api: GridApi<Row>) => {
const result = ex?.importXml(ELEMENTS);
if (!result) return;
api.setRows(result.rows);
say(
`${result.rows.length} rows from the element form. & and CDATA are decoded and nil becomes null.`,
);
},
attributes: (api: GridApi<Row>) => {
const result = ex?.importXml(ATTRIBUTES, { rowTag: 'person' });
if (!result) return;
api.setRows(result.rows);
say(`${result.rows.length} rows from the attribute form (rowTag: 'person').`);
},
doctype: () => {
try {
ex?.importXml(DOCTYPE);
} catch (error) {
const e = error as Error & { code?: string };
say(`Refused (${e.code}): ${e.message}`);
}
},
}; This example needs an extra package. To run it locally, install with npm i @maxgrid/angular @maxgrid/export.
When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
import { Component, OnDestroy } from '@angular/core';
import { GridComponent, isGridEvent, type GridApi, type GridEventMessage } from '@maxgrid/angular';
import { actions, options, setup, type Row } from './example';
@Component({
selector: 'app-example-grid',
standalone: true,
imports: [GridComponent],
template: `
<div class="toolbar">
<button
type="button"
[disabled]="!api"
(click)="api && actions.elements(api)"
>Element form</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.attributes(api)"
>Attribute form</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.doctype(api)"
>Refuse DOCTYPE</button>
</div>
<mx-grid style="height: 300px" [options]="options" [events]="relayed" (gridEvent)="onEvent($event)" />
`,
})
export class ExampleGridComponent implements OnDestroy {
readonly options = options;
readonly actions = actions;
readonly relayed = ['ready'] as const;
api: GridApi<Row> | null = null;
private stop: (() => void) | null = null;
onEvent(event: GridEventMessage<Row>): void {
if (isGridEvent(event, 'ready')) {
this.api = event.payload.api;
this.stop = setup(this.api, console.log);
}
}
ngOnDestroy(): void {
this.stop?.();
}
}import { withExport, type ExportableGridApi } from '@maxgrid/export';
import type { GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string;
export interface Row {
id: number;
name: string;
dept: string;
joined: string | null;
active: boolean;
}
const rows: Row[] = [{ id: 0, name: '(old row)', dept: 'dev', joined: null, active: false }];
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 70 },
{ id: 'name', title: 'Name', width: 150 },
{
id: 'dept',
title: 'Department',
kind: 'select',
width: 120,
editorOptions: [
{ value: 'dev', label: 'Dev' },
{ value: 'ops', label: 'Ops' },
],
optionLabel: (v) => (v === 'dev' ? 'Dev' : 'Ops'),
},
{ id: 'joined', title: 'Joined', kind: 'date', width: 110 },
{ id: 'active', title: 'Active', kind: 'boolean', width: 80 },
],
rows,
rowKey: 'id',
};
const ELEMENTS = `<?xml version="1.0" encoding="UTF-8"?>
<rows>
<row><id>1</id><name>Carter & Co</name><dept>Ops</dept><joined>2024-03-02</joined><active>true</active></row>
<row><id>2</id><name><![CDATA[Emma Stone <Lead>]]></name><dept>Dev</dept><joined nil="true"/><active>false</active></row>
</rows>`;
const ATTRIBUTES =
'<people><person id="3" name="Noah Reed" dept="Dev" joined="2025.1.15" active="yes"/><person id="4" name="Olivia Hayes" dept="Ops"/></people>';
const DOCTYPE =
'<!DOCTYPE rows [<!ENTITY secret SYSTEM "file:///etc/passwd">]><rows><row><name>&secret;</name></row></rows>';
let say: (message: LogText) => void = () => {};
let ex: ExportableGridApi<Row> | undefined;
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
ex = withExport(api);
log(
'importXml(text) reads both the element form and the attribute form. It parses without a DOM, so it also runs in a worker.',
);
}
export const actions = {
elements: (api: GridApi<Row>) => {
const result = ex?.importXml(ELEMENTS);
if (!result) return;
api.setRows(result.rows);
say(
`${result.rows.length} rows from the element form. & and CDATA are decoded and nil becomes null.`,
);
},
attributes: (api: GridApi<Row>) => {
const result = ex?.importXml(ATTRIBUTES, { rowTag: 'person' });
if (!result) return;
api.setRows(result.rows);
say(`${result.rows.length} rows from the attribute form (rowTag: 'person').`);
},
doctype: () => {
try {
ex?.importXml(DOCTYPE);
} catch (error) {
const e = error as Error & { code?: string };
say(`Refused (${e.code}): ${e.message}`);
}
},
};