Skip to main content

How to parse XML output

The XMLOutputParser takes language model output which contains XML and parses it into a JSON object.

The output parser also supports streaming outputs.

Currently, the XML parser does not contain support for self closing tags, or attributes on tags.

Usage

npm install @langchain/core
import { XMLOutputParser } from "@langchain/core/output_parsers";

const XML_EXAMPLE = `<?xml version="1.0" encoding="UTF-8"?>
<userProfile>
<userID>12345</userID>
<name>John Doe</name>
<email>john.doe@example.com</email>
<roles>
<role>Admin</role>
<role>User</role>
</roles>
<preferences>
<theme>Dark</theme>
<notifications>
<email>true</email>
<sms>false</sms>
</notifications>
</preferences>
</userProfile>`;

const parser = new XMLOutputParser();

const result = await parser.invoke(XML_EXAMPLE);

console.log(JSON.stringify(result, null, 2));
/*
{
"userProfile": [
{
"userID": "12345"
},
{
"name": "John Doe"
},
{
"email": "john.doe@example.com"
},
{
"roles": [
{
"role": "Admin"
},
{
"role": "User"
}
]
},
{
"preferences": [
{
"theme": "Dark"
},
{
"notifications": [
{
"email": "true"
},
{
"sms": "false"
}
]
}
]
}
]
}
*/

API Reference:

Streaming

import { XMLOutputParser } from "@langchain/core/output_parsers";
import { FakeStreamingLLM } from "@langchain/core/utils/testing";

const XML_EXAMPLE = `<?xml version="1.0" encoding="UTF-8"?>
<userProfile>
<userID>12345</userID>
<roles>
<role>Admin</role>
<role>User</role>
</roles>
</userProfile>`;

const parser = new XMLOutputParser();

// Define your LLM, in this example we'll use demo streaming LLM
const streamingLLM = new FakeStreamingLLM({
responses: [XML_EXAMPLE],
}).pipe(parser); // Pipe the parser to the LLM

const stream = await streamingLLM.stream(XML_EXAMPLE);
for await (const chunk of stream) {
console.log(JSON.stringify(chunk, null, 2));
}
/*
{}
{
"userProfile": ""
}
{
"userProfile": "\n"
}
{
"userProfile": [
{
"userID": ""
}
]
}
{
"userProfile": [
{
"userID": "123"
}
]
}
{
"userProfile": [
{
"userID": "12345"
},
{
"roles": ""
}
]
}
{
"userProfile": [
{
"userID": "12345"
},
{
"roles": [
{
"role": "A"
}
]
}
]
}
{
"userProfile": [
{
"userID": "12345"
},
{
"roles": [
{
"role": "Admi"
}
]
}
]
}
{
"userProfile": [
{
"userID": "12345"
},
{
"roles": [
{
"role": "Admin"
},
{
"role": "U"
}
]
}
]
}
{
"userProfile": [
{
"userID": "12345"
},
{
"roles": [
{
"role": "Admin"
},
{
"role": "User"
}
]
}
]
}
*/

API Reference:


Help us out by providing feedback on this documentation page: