Woodstox : Validate against XML Schema

Overview This article demonstrates how to validate an XML document against an XML Schema by using Woodstox. Create employeeSchema.xml as shown below. It defines the employee element that contains id, name and salary sub-elements. Create validEmployee.xml as shown below. It is in accordance with employeeSchema.xml described above. Create invalidEmployee.xml as shown below. Note that it … Read more

Woodstox : Validate against DTD

Overview This article demonstrates how to validate an XML document against a DTD by using Woodstox. Create employee.dtd as shown below. It defines the employee element that contains id, name and salary sub-elements. Create validEmployee.xml as shown below. It is in accordance with employee.dtd described above. Create invalidEmployee.xml as shown below. Note that it is … Read more

Woodstox : Bind Namespace Prefix and URI During XML Generation Using StAX

Overview This article demonstrates how Woodstox can be used to bind namespace URI and prefix during XML generation. Create TestBindingPrefix class as shown below. Prefix is bound using the following API methods: XMLStreamWriter2.setPrefix() method for setting the prefix and URI(e.g. see line 28 below) XMLStreamWriter2.setDefaultNamespace() to set the default namespace URI (see line 21 below) … Read more

Woodstox : Generate XML Using StAX

Overview Woodstox StAX Streaming API can be used to generate XML document. The key classes responsible for generating XML content are XMLOutputFactory2 and XMLStreamWriter2 . Create TestGenerateXml class as shown below. Point the outstream to Console output (see line 16 below). The following methods are used for XML content generation: create start element using writeStartElement() … Read more

Woodstox : Coalesce CDATA

Overview This article demonstrates how to optionally coalesce CData content using Woodstox StAX parser. This program shall parse the same XML document twice: once without coalescing and once with coalescing. Create employee.xml as shown below. Note that the name of the employee is mentioned within a CDATA section (see lines 4-6 below). Create TestCoalesceCData as … Read more

Woodstox : Verify Empty Element Content

Overview This article demonstrates how to verify whether an element is empty or not using Woodstox StAX parser. Create employee.xml as shown below. Note that the element ‘address’ is empty. Create TestVerifyEmptyElement class as shown below. Note that start element is obtained by using the StAX API (see lines 20-26 below). Verify that the ‘address’ … Read more

Woodstox : Read Namespace Of XML Start Element

Overview This article demonstrates how to read namespace related information using Woodstox StAX parser. Create employee.xml as shown below. Note the namespace URI ‘https://studytrails.com’ (see line 1 below) and the namespace prefix ‘s’ used in all start and end elements. Create TestReadNamespace class as shown below. Note that start element is obtained by using the … Read more

Woodstox : Read Attribute Of XML Element

Overview This article demonstrates how to read the attribute in an XML element using Woodstox. Create employee.xml as shown below. Note the attribute ‘id’ for the element ’employee’ (see line 1 below). Create TestReadAttribute class as shown below. Note that start element is obtained by using the StAX API (see lines 20-28 below). The following … Read more

Woodstox : Peek to Next Element

Overview Woodstox provides a feature to peek into the next XML element from the current XML element. This is achieved by using XMLEventReader.peek() method. This sample program demonstrates how to peek into the next XML element. Create the employee.xml as shown below. Note that there are three XML elements: start element ’employee’ character content ‘some … Read more

java-xml-woodstox-stax-replace-entity-reference

Overview XML documents can refer to entities defined in DTD. Woodstox provides support for replacing the entity reference with its corresponding value. The following program explores Woodstox’ feature of replacing entity references. Create the employee.dtd as shown below. In particular, note that the entity ‘commonAddress’ is defined with corresponding value ‘Beth Street, Cathy County, Dessert … Read more