Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 36 ยท XML and SOAP Messages

XML Basics for SOAP

Learn the XML you need for SOAP: elements, attributes, well-formedness rules, escaping, CDATA and encoding, and build a small parser in JavaScript that reads SOAP messages.

Every SOAP message is an XML document. You do not need to master the whole XML family to work with SOAP, but you must read XML fluently, know what makes a document valid for a parser, and avoid the handful of mistakes that break messages in production. This lesson covers exactly that and ends with a small parser you can reuse in the rest of the track.

Elements, attributes and text

An XML document is a tree. Each element has a name, optional attributes, and children that are elements or text.

XML
<order id="17" currency="EUR">
  <customer>Ada Lovelace</customer>
  <item sku="T-100" quantity="2">Tea</item>
  <note/>
</order>
  • order is the root element. A document has exactly one.
  • id="17" is an attribute: a name and a quoted value on the start tag.
  • <note/> is an empty element, a shorthand for <note></note>.
  • Whitespace between elements is usually insignificant, but text inside <customer> is data.

Well-formedness

A document must be well-formed or a parser rejects it completely. The rules are short:

  1. Exactly one root element.
  2. Every start tag has a matching end tag, or is self-closing.
  3. Elements nest properly: <a><b></a></b> is an error.
  4. Attribute values are quoted (single or double), and an attribute name appears once per element.
  5. Names are case sensitive: <Item> and <item> differ.
  6. The characters < and & are not allowed inside text; they must be escaped.

SOAP processors are strict about these. One unescaped ampersand in a customer name means the whole message is rejected with a fault. That is the reason "build XML by gluing strings" is dangerous.

The five predefined entities

Write To mean
&lt; <
&gt; >
&amp; &
&quot; " (needed inside double-quoted attributes)
&apos; '

For a long block with many special characters, such as an embedded piece of HTML or code, use a CDATA section: everything inside is raw text.

XML
<script><![CDATA[ if (a < b && b > 0) { run(); } ]]></script>

Only the sequence ]]> cannot appear inside a CDATA section.

The XML declaration and encoding

The first line of many messages is the XML declaration:

XML
<?xml version="1.0" encoding="UTF-8"?>

It must be the very first thing in the document (no blank line or byte before it), and encoding must match the real bytes. SOAP messages should be UTF-8 (or UTF-16). A common bug: a service writes Latin-1 bytes but declares UTF-8, so รฉ arrives broken.

Escaping and unescaping in code

The following program escapes values correctly and shows what goes wrong when you do not. The escape function is the one to keep.

Node.js Escaping text and attributes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

A small XML parser

Soon you will read SOAP responses, so you need a parser. Real projects use a library (fast-xml-parser, DOMParser, lxml, javax.xml), but our editor has no packages, so here is a compact one that handles elements, attributes, text, entities and CDATA. It returns a tree of { name, attrs, children, text } nodes. We reuse it in later lessons.

Node.js parseXml: a reusable mini parser

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

This parser is not complete: it ignores DTDs, does not check that tags nest correctly, and does not process namespaces. XML Namespaces in SOAP adds the last piece.

Never parse untrusted XML with a parser that resolves external entities or DTDs. A malicious message can read local files or exhaust memory (the "XXE" and "billion laughs" attacks). Turn DTD processing off in real parsers, and note that SOAP messages should not contain DTDs at all; the SOAP specification forbids them.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which of these is a well-formed XML fragment?

    Option 1 nests wrongly, option 2 has an unquoted attribute, option 4 changes the case of the end tag. Only the third is well-formed.
  2. 2. How do you write an ampersand inside XML text?

    A raw & starts an entity reference, so text must use &.
  3. 3. What is a CDATA section for?

    Inside <![CDATA[ ... ]]> special characters such as < and & need no escaping.
  4. 4. Why should SOAP processors disable DTD and external entity processing?

    External entities can read files or call URLs, and nested entities can exhaust memory. SOAP messages must not contain DTDs.

Summary

  • A SOAP message is an XML tree: root element, nested elements, attributes and text.
  • Well-formedness is strict: one root, matched tags, quoted attributes, escaped < and &.
  • Escape every value you place into XML; use CDATA for raw blocks.
  • Match the declared encoding to the real bytes; prefer UTF-8.
  • Disable DTD and external-entity processing when parsing untrusted XML.

Next: XML Namespaces in SOAP