Converts a Python dictionary or other native data type into a valid XML string.
86
Convert inbound records into XML while keeping element names valid and text content safely escaped.
{"summary": "Use < & > symbols"} produces XML with an XML declaration (unless the caller disables it), a root element named after the provided root (default payload), and a <summary> element whose text reads Use < & > symbols. @testname attribute. For example, {"123 status": "ok", "product name": "Road Bike"} yields elements that preserve those keys while keeping tag names XML-safe inside the root wrapper. @test{"order": {"line items": [{"sku": "AB&C", "detail": "Steel <frame>"}], "notes": "Rush"}}, the output contains nested elements for the list and child dictionaries, with text values escaped (e.g., Steel <frame>) and without invalid tag names (e.g., line items not appearing verbatim as a tag). @test@generates
from typing import Any, Mapping
def serialize_payload(payload: Mapping[str, Any], *, root_name: str = "payload", include_declaration: bool = True) -> bytes:
"""Convert the payload into XML bytes with valid element names and escaped text; root tag defaults to 'payload'."""Converts Python data structures into XML with safe tag naming and escaped values.
Install with Tessl CLI
npx tessl i tessl/pypi-dicttoxmldocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10