Laboratorium 1 - Przetwarzanie dokumentów XML w języku Java
Aplikacja
plik OPALab1.java
1 import java.util.*;
2 import java.io.*;
3 import org.xml.sax.*;
4 import org.xml.sax.helpers.DefaultHandler;
5 import javax.xml.parsers.SAXParserFactory;
6 import javax.xml.parsers.ParserConfigurationException;
7 import javax.xml.parsers.SAXParser;
8 import javax.xml.parsers.DocumentBuilder;
9 import javax.xml.parsers.DocumentBuilderFactory;
10 import org.w3c.dom.*;
11
12 import javax.xml.transform.Transformer;
13 import javax.xml.transform.TransformerFactory;
14 import javax.xml.transform.TransformerException;
15 import javax.xml.transform.TransformerConfigurationException;
16 import javax.xml.transform.dom.DOMSource;
17 import javax.xml.transform.stream.StreamSource;
18 import javax.xml.transform.stream.StreamResult;
19
20 public class OPALab1{
21 public static void main(String[] args){
22 switch(Integer.parseInt(args[0])){
23 case 1:
24 System.out.println("*** OPALab1:SAXParser");
25
26 DefaultHandler handler = new SAXProductList();
27 SAXParserFactory saxFactory = SAXParserFactory.newInstance();
28 try{
29 SAXParser saxParser = saxFactory.newSAXParser();
30 saxParser.parse(args[1], handler);
31 }catch(Throwable t){
32 t.printStackTrace();
33 }
34 ((SAXProductList)handler).printSAXProductList();
35 break;
36
37 case 2:
38 System.out.println("*** OPALab1:MakerDOMParser");
39 MakerDOMParser MakerDOMParser=new MakerDOMParser(args[1]);
40 DOMProductList productList=new DOMProductList(MakerDOMParser.getDocument());
41 productList.printDOMProductList();
42 break;
43
44 case 3:
45 System.out.println("*** OPALab1:XSLT");
46 try{
47 File datafile = new File(args[1]);
48 File stylesheet = new File(args[2]);
49 File resultfile = new File("result.html");
50
51 DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
52 DocumentBuilder DOMbuilder = domFactory.newDocumentBuilder();
53 Document document = DOMbuilder.parse(datafile);
54
55 StreamSource stylesource = new StreamSource(stylesheet);
56 TransformerFactory tFactory = TransformerFactory.newInstance();
57 Transformer transformer = tFactory.newTransformer(stylesource);
58
59 DOMSource source = new DOMSource(document);
60 StreamResult result = new StreamResult(resultfile);
61
62 transformer.transform(source, result);
63 }catch (Exception exc){
64 System.out.println(exc);
65 }
66 break;
67 }
68 }
69 }
plik Product.java
1 import java.util.*;
2 import java.io.*;
3
4 public class Product{
5 public static boolean PRODUCT_DEBUG_FUNCTION=false;
6
7 public String symbol;
8 public String nazwa;
9 public String cena;
10
11 public Product(String symbol, String nazwa, String cena){
12 if(PRODUCT_DEBUG_FUNCTION) System.out.println("*** Product(" + symbol + ", " + nazwa + ", " + cena + ")");
13
14 this.symbol = symbol;
15 this.nazwa = nazwa;
16 this.cena = cena;
17 }
18
19 public void printProduct(){
20 System.out.println("symbol=" + symbol + " nazwa=" + nazwa + " cena=" + cena);
21 }
22 }
23
Parsowanie pliku XML za pomocą SAX
plik SAXProductList.java
1 import java.util.*;
2 import java.io.*;
3 import org.xml.sax.*;
4 import org.xml.sax.helpers.DefaultHandler;
5 import javax.xml.parsers.SAXParserFactory;
6 import javax.xml.parsers.ParserConfigurationException;
7 import javax.xml.parsers.SAXParser;
8
9 public class SAXProductList extends DefaultHandler{
10 public static boolean SAX_DEBUG_FUNCTION=false;
11
12 List productList = new ArrayList();
13 String tag="startDocument";
14
15 String symbol;
16 String cena;
17 String nazwa;
18
19 public void startDocument() throws SAXException{
20 if(SAX_DEBUG_FUNCTION) System.out.println("*** SAX:startDocument");
21 }
22
23 public void startElement(String namespaceURI, String sName, String qName, Attributes attrs) throws SAXException{
24 if(SAX_DEBUG_FUNCTION) System.out.println("*** SAX:startElement sName=" + sName + " qName=" + qName);
25
26 if(qName=="grupa"){
27 System.out.println("groupa:nazwa=" + attrs.getValue("nazwa"));
28 }
29
30 tag=qName;
31 }
32
33 public void endElement(String namespaceURI, String sName, String qName) throws SAXException{
34 if(SAX_DEBUG_FUNCTION) System.out.println("*** SAX:endElement sName=" + sName + " qName=" + qName);
35 tag="endElement";
36
37 if(qName=="produkt") productList.add(new Product(symbol, nazwa, cena));
38 }
39
40 public void characters(char ch[], int start, int length){
41 if(SAX_DEBUG_FUNCTION) System.out.println("*** SAX:characters");
42
43 String value = new String(ch, start, length);
44
45 if(tag=="symbol") symbol=value;
46 if(tag=="cena") cena=value;
47 if(tag=="nazwa") nazwa=value;
48 }
49
50 public void endDocument() throws SAXException{
51 if(SAX_DEBUG_FUNCTION) System.out.println("*** SAX:endDocument");
52 }
53
54 public void printSAXProductList(){
55 for(int i=0; i<productList.size(); i++){
56 Product p=((Product)productList.get(i));
57 p.printProduct();
58 }
59 }
60 }
61
Parsowanie pliku XML za pomocą DOM
1 import java.util.*;
2 import java.io.*;
3 import javax.xml.parsers.ParserConfigurationException;
4 import javax.xml.parsers.DocumentBuilder;
5 import javax.xml.parsers.DocumentBuilderFactory;
6 import org.w3c.dom.*;
7
8 public class MakerDOMParser{
9 Document document;
10
11 public MakerDOMParser(String xmlFileName){
12 System.out.println("*** MakerDOMParser(" + xmlFileName + ")");
13 try{
14 DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
15 DocumentBuilder builder = domFactory.newDocumentBuilder();
16 document = builder.parse(new File(xmlFileName));
17 }catch(Exception exc){
18 System.out.println(exc);
19 }
20 }
21
22 Document getDocument(){ return document; }
23 }
24
plik DOMProductList.java
1 import java.util.*;
2 import java.io.*;
3
4 import javax.xml.parsers.ParserConfigurationException;
5 import javax.xml.parsers.DocumentBuilder;
6 import javax.xml.parsers.DocumentBuilderFactory;
7 import org.w3c.dom.*;
8
9 import java.io.IOException;
10 import org.xml.sax.SAXException;
11 import javax.xml.parsers.*;
12
13 public class DOMProductList{
14 List productList;
15
16 public DOMProductList(Document xmlDocument){
17 productList=new ArrayList();
18
19 System.out.println("*** DOMProductList(xmlDocument)");
20
21 /** get attributes of group */
22 NodeList groupNodeList = xmlDocument.getElementsByTagName("grupa");
23 Node groupNode=groupNodeList.item(0);
24
25 int len = (groupNode.getAttributes()==null) ? 0: groupNode.getAttributes().getLength();
26 Attr attributes[] = new Attr[len];
27
28 for(int i=0; i<len; i++){
29 attributes[i] = (Attr)groupNode.getAttributes().item(i);
30 System.out.println("grupa:" + attributes[i].getName() + "=" + attributes[i].getValue());
31 }
32
33 /** get content */
34 NodeList productNodeList = xmlDocument.getElementsByTagName("produkt");
35 System.out.println("sizeof(product): " + productNodeList.getLength());
36
37 String symbol=new String();
38 String nazwa=new String();
39 String cena=new String();
40
41 String value=new String();
42 String tag=new String();
43
44 int ntype;
45
46 for(int i=0; i<productNodeList.getLength(); i++){
47 Node productNode = productNodeList.item(i);
48 NodeList productNodeChildren = productNode.getChildNodes();
49
50 for(int j=0; j < productNodeChildren.getLength(); j++){
51 Node productChildNode = productNodeChildren.item(j);
52
53 ntype=productChildNode.getNodeType();
54
55 if(ntype!=Node.TEXT_NODE){
56 value=productChildNode.getFirstChild().getNodeValue();
57 tag=productChildNode.getNodeName();
58 //System.out.println("nodeType=" + ntype + " tag=" + tag + " value=" + value);
3 }
4
5 if(tag=="symbol") symbol=value;
6 if(tag=="cena") cena=value;
7 if(tag=="nazwa") nazwa=value;
8 }
9 productList.add(new Product(symbol, nazwa, cena));
10 }
11 }
12
13 public void printDOMProductList(){
14 for(int i=0; i<productList.size(); i++){
15 Product p=((Product)productList.get(i));
16 p.printProduct();
17 }
18 }
19 }
20
Transformacja XSLT
Twój komentarz:
Komentarze: