---
slug: "build-xml-via-xml-etree-elementtree"
title: "Creating and Posting XML with Python"
description: "Replace legacy log analyzers (Webalizer / Analog) with GoAccess for Apache / nginx access logs — sample configuration and example output."
url: "https://www.ytyng.com/en/blog/build-xml-via-xml-etree-elementtree"
publish_date: "2021-10-10T09:45:45Z"
created: "2021-10-10T09:45:45Z"
updated: "2026-05-11T13:21:32.727Z"
categories: ["Python"]
keywords: ""
featured_image_url: "https://media.ytyng.com/resize/20230812/78b059ee156744eea8ad6b186bcabef2.png.webp?width=768"
has_video: false
has_music: false
video_urls: []
music_urls: []
lang: "en"
---

# Creating and Posting XML with Python

<p>Create and Post XML with Python</p>
<p></p>
<pre>from xml.etree import ElementTree

request_xml = ElementTree.Element(<span>'request'</span>)
update_request = ElementTree.SubElement(
    request_xml, <span>'itemUpdateRequest'</span>)
item = ElementTree.SubElement(update_request, <span>'item'</span>)
ElementTree.SubElement(item, <span>'itemUrl'</span>).text = itemUrl

<span>for </span>key, value <span>in </span>updateData.items():<br />    ElementTree.SubElement(item, key).text = value

response = <span>self</span>.request(
    <span>'/es/1.0/item/update'</span>,
    <span>method</span>=<span>'post'</span>,
    <span>headers</span>={<span>'Content-Type'</span>: <span>'application/xml'</span>},
    <span>data</span>=ElementTree.tostring(
        request_xml, <span>encoding</span>=<span>'utf-8'</span>, <span>xml_declaration</span>=<span>True</span>)
)</pre>
