Ultralytics YOLO27: Join the waitlist.
License YOLO
Back to Ultralytics Glossary

XML

XML is a text format that nests custom tags into a tree of structured data. Its syntax, schemas, PASCAL VOC annotations, and comparison with JSON and YAML.

XML (Extensible Markup Language) is a text format for structured data in which custom tags nest inside one another to form a tree, such as a <size> element containing <width> and <height>. The World Wide Web Consortium published XML 1.0 as a Recommendation in February 1998, as a simplified subset of SGML (Standard Generalized Markup Language) for exchanging documents and data on the web. XML has no predefined tags, unlike HTML: each application defines its own tags for what its data means, which is why so many file formats are built on it, from SVG graphics and RSS feeds to Microsoft Office documents. In machine learning and computer vision, XML appears mostly in annotation files, model files, and the healthcare and enterprise systems that supply training data.

XML Syntax and Validation#

An XML document has exactly one root element. Elements can hold text, child elements, and attributes written inside the opening tag, as in <box label="car" xtl="10">, and namespaces declared with xmlns keep tags from different vocabularies apart. A document that follows the syntax rules, with every tag closed, elements properly nested, and attribute values quoted, is well-formed, and parsers must stop at the first well-formedness error. A document is also valid when it matches a schema, written as a Document Type Definition (DTD) or an XML Schema (XSD) that lists the allowed elements, their order, and their data types. XPath expressions such as size/width select nodes from the tree, and XSLT stylesheets transform one XML vocabulary into another.

XML in Computer Vision and Machine Learning#

The PASCAL VOC challenge, held from 2005 to 2012, made XML the classic annotation format for object detection. Each image has one XML file with its file name and size and an <object> element per object, holding the class name, truncated and difficult flags, and a <bndbox> whose pixel corners xmin, ymin, xmax, and ymax define the bounding box. The LabelImg annotation tool saves this format by default, so many datasets still ship with it and need converting during data preprocessing. The CVAT for images 1.1 export from CVAT stores every annotation of a task in a single XML file, with boxes written as attributes such as xtl, ytl, xbr, and ybr.

XML also describes models and records. Intel's OpenVINO Intermediate Representation stores a network's layers and connections in an .xml file and its weights in a matching .bin file, and OpenCV's pretrained Haar cascade face detectors are distributed as XML. HL7 FHIR health records, which clinical AI projects pair with images for medical image analysis, can be exchanged as XML or JSON, and Word and Excel files are ZIP archives of XML parts that document AI systems read.

This example parses a VOC annotation with Python's built-in ElementTree and converts each box to the normalized center, width, and height of the YOLO label format:

import xml.etree.ElementTree as ET

# A PASCAL VOC annotation: one XML file per image, one <object> per box
voc = """
<annotation>
  <filename>000017.jpg</filename>
  <size><width>480</width><height>364</height><depth>3</depth></size>
  <object><name>person</name><difficult>0</difficult>
    <bndbox><xmin>185</xmin><ymin>62</ymin><xmax>279</xmax><ymax>199</ymax></bndbox></object>
  <object><name>horse</name><difficult>0</difficult>
    <bndbox><xmin>90</xmin><ymin>78</ymin><xmax>403</xmax><ymax>336</ymax></bndbox></object>
</annotation>
"""
root = ET.fromstring(voc)
w, h = int(root.find("size/width").text), int(root.find("size/height").text)

# Convert corner pixels to YOLO's normalized center x, center y, width, height
for obj in root.iter("object"):
    box = obj.find("bndbox")
    x1, y1, x2, y2 = (float(box.find(k).text) for k in ("xmin", "ymin", "xmax", "ymax"))
    cx, cy = ((x1 + x2) / 2 - 1) / w, ((y1 + y2) / 2 - 1) / h  # VOC pixel indices start at 1
    print(obj.find("name").text, f"{cx:.3f} {cy:.3f} {(x2 - x1) / w:.3f} {(y2 - y1) / h:.3f}")

It prints person 0.481 0.356 0.196 0.376 and horse 0.511 0.566 0.652 0.709: the same two boxes as fractions of the image size, ready to be written as label lines after their class indices. The Ultralytics VOC.yaml dataset file runs this conversion automatically on first use, downloading VOC 2007 and 2012, parsing every XML annotation with ElementTree, skipping objects marked difficult, and writing YOLO .txt labels for 16,551 training and 4,952 validation images.

XML vs. JSON vs. YAML#

  • JSON: Objects, arrays, strings, numbers, and booleans with lighter syntax than XML and direct mapping to Python dictionaries and JavaScript objects. Web APIs and the COCO dataset annotations use it.
  • YAML: Indentation in place of closing tags, support for comments, and a syntax that, since YAML 1.2, accepts any JSON document, which makes it popular for hand-edited configuration such as YOLO dataset and model files.
  • XML: More verbose, with schemas, namespaces, attributes, and mixed content, meaning text with inline markup, which suits documents and standards bodies that need strict validation.

Parsing Untrusted XML#

XML's entity feature lets a document define reusable text, and attackers abuse it. In the "billion laughs" attack, about ten nested entity definitions, each referencing the previous one ten times, expand a file of a few hundred bytes into gigabytes of text. An XML external entity (XXE) attack declares an entity that points to a local file or internal URL, so a vulnerable parser reads secrets into the document or sends requests from inside the network. Python's xml.etree.ElementTree does not expand external entities and raises a parse error when it meets one, and its Expat parser resists billion laughs from version 2.4.1 on. The defusedxml package goes further and raises EntitiesForbidden for any entity declaration, internal or external.

Explore solutions

Computer vision in sports

Computer vision in sports

Discover how Ultralytics YOLO powers sports vision with player and ball tracking, performance analytics, coaching insights, and athlete rehabilitation.
Learn more
Computer vision for aerial imagery

Computer vision for aerial imagery

Turn drone and aerial imagery into real-time insights for agriculture, aquaculture, conservation, and geospatial analysis with Ultralytics YOLO.
Learn more
Computer vision in aerospace

Computer vision in aerospace

Apply computer vision in aerospace with Ultralytics YOLO for aircraft inspection, runway FOD detection, airport ground operations, and composite defect detection.
Learn more
Computer vision in security

Computer vision in security

Protect every site with Ultralytics YOLO models. Vision AI powers perimeter monitoring, threat detection, license-plate recognition, and workplace safety.
Learn more
Computer vision in robotics

Computer vision in robotics

Power smarter machines with Ultralytics YOLO models. Vision AI in robotics drives autonomous navigation, perception, object tracking, and real-time control.
Learn more
Computer vision in logistics

Computer vision in logistics

Streamline logistics with Ultralytics YOLO models. Vision AI enables package inspection, sorting, vehicle tracking, and real-time warehouse safety monitoring.
Learn more
Computer vision in retail

Computer vision in retail

Reimagine retail with Ultralytics YOLO models. Vision AI powers inventory tracking, shelf monitoring, queue management, and smarter customer insights.
Learn more
Computer vision in healthcare

Computer vision in healthcare

Build healthcare solutions with Ultralytics YOLO models. Vision AI in healthcare powers faster medical imaging, smarter diagnostics, and patient monitoring.
Learn more
Computer vision in manufacturing

Computer vision in manufacturing

Optimize manufacturing with Ultralytics YOLO models. Vision AI drives quality control, defect detection, PPE compliance, and assembly line automation.
Learn more
Computer vision in automotive

Computer vision in automotive

Apply computer vision in automotive with Ultralytics YOLO models. Vision AI elevates road safety, driver assistance, and vehicle automation for smarter roads.
Learn more
Computer vision in agriculture

Computer vision in agriculture

Bring vision AI to smart agriculture with Ultralytics YOLO models. Power crop monitoring, livestock tracking, and precision farming for higher, smarter yields.
Learn more

Let's build the future of AI together!

Begin your journey with the future of machine learning