Simple API For XML - Example

Example

Given the following XML document:

¶ Some Text Pre-Text Inlined text Post-text.

This XML document, when passed through a SAX parser, will generate a sequence of events like the following:

  • XML Element start, named DocumentElement, with an attribute param equal to "value"
  • XML Element start, named FirstElement
  • XML Text node, with data equal to "¶ Some Text" (note: certain white spaces can be changed)
  • XML Element end, named FirstElement
  • Processing Instruction event, with the target some_pi and data some_attr="some_value" (the content after the target is just text; however, it is very common to imitate the syntax of XML attributes, as in this example)
  • XML Element start, named SecondElement, with an attribute param2 equal to "something"
  • XML Text node, with data equal to "Pre-Text"
  • XML Element start, named Inline
  • XML Text node, with data equal to "Inlined text"
  • XML Element end, named Inline
  • XML Text node, with data equal to "Post-text."
  • XML Element end, named SecondElement
  • XML Element end, named DocumentElement

Note that the first line of the sample above is the XML Declaration and not a processing instruction; as such it will not be reported as a processing instruction event (although some SAX implementations provide a separate event just for the XML declaration).

The result above may vary: the SAX specification deliberately states that a given section of text may be reported as multiple sequential text events. Many parsers, for example, return separate text events for numeric character references. Thus in the example above, a SAX parser may generate a different series of events, part of which might include:

  • XML Element start, named FirstElement
  • XML Text node, with data equal to "¶" (the Unicode character U+00b6)
  • XML Text node, with data equal to " Some Text"
  • XML Element end, named FirstElement

Read more about this topic:  Simple API For XML

Famous quotes containing the word example:

    Our intellect is not the most subtle, the most powerful, the most appropriate, instrument for revealing the truth. It is life that, little by little, example by example, permits us to see that what is most important to our heart, or to our mind, is learned not by reasoning but through other agencies. Then it is that the intellect, observing their superiority, abdicates its control to them upon reasoned grounds and agrees to become their collaborator and lackey.
    Marcel Proust (1871–1922)