본문으로 건너뛰기Skip to content
예제 첫화면Examples home

XML 글자를 행으로 읽어 넣기Read XML text into rows

importXml(text, options)는 뿌리 아래 요소 하나를 행 하나로 읽어요. 칸은 자식 요소(<id>1</id>)나 속성(<person id="3"/>)이고 &amp; 같은 기본 엔티티, 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 &amp;, 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.

활용Intermediate
직접 써 보기Live previewXML 글자를 행으로 읽어 넣기Read XML text into rows

코드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)

빌드 도구(Vite 등)를 쓰는 프로젝트용 모듈 파일이에요. 설치 없이 바로 돌려 보려면 HTML 탭이나 직접 해보기를 쓰세요.

이 예제는 추가 패키지가 필요해요. 내 컴퓨터에서 돌리려면 npm i maxgrid @maxgrid/export로 설치하세요.

버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.

index.html
<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>
main.js
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));
example.js
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>김하늘 &amp; 팀</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}행. &amp;·CDATA가 풀리고 nil은 null이에요.`,
      en: `${result.rows.length} rows from the element form. &amp; 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}` });
    }
  },
};

Module files for a project with a bundler such as Vite. To run it with no setup, use the HTML tab or Try it.

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.

index.html
<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>
main.js
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));
example.js
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 &amp; 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. &amp; 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}`);
    }
  },
};