
Product
Introducing Socket MCP for Claude Desktop
Add secure dependency scanning to Claude Desktop with Socket MCP, a one-click extension that keeps your coding conversations safe from malicious packages.
This parser implements a stack-based algorithm to analyze XML hierarchies, tracking parent-child relationships and structure depth. It offers flexible configurations for extracting specific tags (both single and lists) at defined depths, with a rigorous validation system. The implementation is optimized for small XML files (typically a few KB) with simple hierarchical structures (maximum 3-4 levels of depth).
The parser handles:
Important Note: The ParseResult
structure is flat. All configured tags, including those defined as "children" in the configuration, are accessible as first-level attributes of the result
object. There is no nested hierarchical structure.
Elements extracted without warnings or errors.
XML:
<thinking>This is a test</thinking>
Config: {'thinking': 'single'}
Output:
result.thinking → "This is a test"
XML:
<step>A</step>
<step>B</step>
<step>C</step>
Config: {'step': 'list'}
Output:
result.step → ["A", "B", "C"]
XML:
<exercise>
<question>What is 2+2?</question>
<answer>The answer is 4</answer>
</exercise>
Config:
config = {
'exercise': {
'type': 'single',
'children': {
'question': 'single',
'answer': 'single'
}
}
}
Output:
result.exercise → "<question>What is 2+2?</question><answer>The answer is 4</answer>"
result.question → "What is 2+2?"
result.answer → "The answer is 4"
LLM Input:
<thinking>Analysis</thinking>
Answer: 42
After Pre-Processing:
<root>
<thinking>Analysis</thinking>
Answer: 42
</root>
Note: Preprocessing always adds an artificial root tag, since LLM output often does not include a valid root tag. The root tag is shown here only to illustrate the handling of unlabeled text.
Output:
result.thinking → "Analysis"
result.untagged → "Answer: 42"
Elements extracted with warnings in logs and accessible via ParseResult.warnings
XML:
<thinking>Use <formula>E=mc²</formula></thinking>
Config: {'thinking': 'single'}
Output:
result.thinking → "Use <formula>E=mc²</formula>"
# Log and warnings: "Unconfigured tag <formula> found inside 'thinking'"
XML:
<step>
A<detail>some info</detail>
</step>
<step>B</step>
Config: {'step': 'list'}
Output:
result.step → ["A<detail>some info</detail>", "B"]
# Log and warnings: "Unconfigured tag <detail> found inside 'step'"
XML:
<step>Single step</step>
Config: {'step': 'list'}
Output:
result.step → ["Single step"]
# Log and warnings: "List <step> contains only 1 element."
Interrupt execution by raising specific exceptions.
XML: <answer>42</answer>
Config: {'thinking': 'single'}
Error:
XMLStructureError: Tag <thinking> not found (single required).
XML:
<answer>42</answer>
<answer>43</answer>
Config: {'answer': 'single'}
Error:
XMLStructureError: Multiple <answer> found, but 'single' is required.
XML: <answer>stuff</answer>
Config: {'steps': 'list'}
Error:
XMLStructureError: List <steps> is empty (1+ elements required).
XML:
<thinking>Test<thinking>
or
<a><b></a></b>
Error:
XMLFormatError: Unclosed tags remain. Malformed XML structure.
or
XMLFormatError: Mismatched tags: opened <b>, but closed </a>
The parser is designed to be minimalist and efficiently handle simple XML output from LLMs. The following cases are not supported:
Namespaces
<ns:thinking>Test</ns:thinking>
Tags with Special Characters
<distro_linux>Test</distro_linux>
(underscore supported)<foo-bar>Test</foo-bar>
<foo bar>Test</foo bar>
CDATA and Complex Content
<thinking><![CDATA[<test>]]></thinking>
XML Attributes
<tag attr="val">Test</tag>
Normal Mode (Default, strict_mode=False
)
Strict Mode (strict_mode=True
)
FAQs
A minimal XML parser for structured output from LLM
We found that llm_xml_parser demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Add secure dependency scanning to Claude Desktop with Socket MCP, a one-click extension that keeps your coding conversations safe from malicious packages.
Product
Socket now supports Scala and Kotlin, bringing AI-powered threat detection to JVM projects with easy manifest generation and fast, accurate scans.
Application Security
/Security News
Socket CEO Feross Aboukhadijeh and a16z partner Joel de la Garza discuss vibe coding, AI-driven software development, and how the rise of LLMs, despite their risks, still points toward a more secure and innovative future.