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.
Program ...lab06.SAXFormatter (making use of FormattingHandler) prints out a given document (element tags and text nodes only, we do not handle special characters, etc.).
String.trim Printing also comments is not so obvious in this case. We would have to use LexicalHandler.
(Przykładowy dokument i tak jest po polsku...)
W pakiecie ...lab06.sklep znajdziecie program ZnajdzMaxCene oraz niedokończoną klasę MaxCenaHandler,
cena.W pakiecie ...lab06.sklep znajdziecie program Filtruj
oraz niedokońcozną klasę FiltrKategorii.
MaxCenaHandler oraz FiltrKategorii do zbudowania programu, który znajduje maksymalną cenę towaru w podanej kategorii. Wystarczy połączyć parser, filtr i handler w jeden łańcuszek i uruchomić parsowanie. W razie wątpliwości zobacz program SaxFiltry w przykładach.Streaming API fo XML offers more intuitive control flow than SAX.
It also directly supports writing documents – see XMLStreamWriter and XMLEventWriter classes.
Tworzenie Readara i Writera - może się przydać do ostatniego punktu poniższego zadania.
XMLInputFactory ifa = XMLInputFactory.newInstance(); XMLEventReader reader = ifa.createXMLEventReader(new FileInputStream(wej)); XMLOutputFactory ofa = XMLOutputFactory.newInstance(); XMLEventWriter writer = ofa.createXMLEventWriter(new FileOutputStream(wyj)); XMLEventFactory ef = XMLEventFactory.newInstance();
W pakiecie ...lab06.sklep znajdziecie program SredniaCena_Stax
oraz klasę FiltrKategorii_Stax.
Uwaga, działają one na wersji dokumentów z przestrzeniami nazw (sklep_ns.xml).
XMLEventWriter, aby stworzyć program, który przepisuje plik zostawiając tylko towary z wybranej kategorii.
Niewykluczone, że filtr trzeba jeszcze poprawić/uzupełnić