Examples for XML processing in different programming languages.
The exercises below are simple instances of common real-world XML processing tasks: selection of elements, modification of element content, modification of the XML tree and validation against an XML schema. The example XML file and the underlying schema can be found in the xml directory.
Feel free to send pull requests for solutions in additional languages or for improvements of the existing solutions!
Print a list of all leaf node creators and titles, i.e., all content/creator
and content/title
children of node
elements that contain no other node
elements as children. Each item should be a line of the form "creator, title".
Same as exercise 1, but consider the case where some leaf nodes have no creator element. Search their ancestors until a creator element is found and print that instead.
Make the content of all title
elements uppercase. Print the resulting XML
document.
Add a new item to the end of the "A Song of Ice and Fire" section with title "A Feast for Crows" and date "2005". Print the resulting XML document.
Remove the "Programming Books" section. Print the resulting XML document.
Validate the XML document. Keep in mind that the schema file references additional external schema files that have to be retrieved from the internet, so internet access is required for validation.
Flatten the XML hierarchy by removing the top level nodes (those immediately
below the tree
root element). Add a content/subject
element to all their
immediate child nodes that preserves the name
attribute value of the removed
parent node ("Programming Books" or "Fantasy Books", respectively). Print the
resulting XML document.
Reverse exercise 7: Create a new top level node for each distinct subject found in the current top level nodes. Move each node to its respective parent node (one of the new top level nodes). Remove the subject elements. Print the resulting XML document.