org.w3c.dom
and javax.xml
Import the project from XML05.zip to Eclipse. Look at the lecture examples in
pl.mimuw.xml.rooms
package. Run them on the example file rooms1.xml.
Program DOMFormatter prints out a given document (element tags and text nodes only, we do not handle special characters, etc.).
String.trim
The following lines of code turn validation against XML Schema on.
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = schemaFactory.newSchema(new StreamSource("school.xsd")); factory.setSchema(schema); DocumentBuilder db = factory.newDocumentBuilder();
projector
to false
, to true
and run the program each time.ErrorHandler
and setErrorHandler
to register it before parsing.The following line of code makes the parser namespace aware.
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder db = factory.newDocumentBuilder();
Basing on CountSeats_DOM_Specialized create a program that...
DOM allows us to modify the values of nodes and the structure of the tree.
setTextContent
is the easiest way to set the new value of a simple (text-only) element,
and setAttribute
sets the value of an attribute.
Creating new nodes requires 1) to create the object using one of
factory methods of the owner Document
, and then
2) to add the object to the tree using appendChild
or insertChild
methods of the parent element.
(optional) Add the calculated information about the number of seats to the document in
a new element result
, as a child of rooms
.
Write the document to a new file or to stdout using the methods that can be found e.g. in
...more_dom.DomLoadSave2
.