Created
November 20, 2013 09:00
-
-
Save msulima/7559976 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.xml.parsers.SAXParser; | |
import javax.xml.parsers.SAXParserFactory; | |
import org.xml.sax.Attributes; | |
import org.xml.sax.SAXException; | |
import org.xml.sax.helpers.DefaultHandler; | |
import java.util.HashMap; | |
import java.util.Map; | |
public class Zad1 { | |
//private static final String FILE = "C:\\Documents and Settings\\Administrator\\Pulpit\\mkyong.xml"; | |
private static final String FILE = "C:\\Documents and Settings\\Administrator\\Pulpit\\sredniowiecze.xml"; | |
public static void main(String argv[]) { | |
try { | |
SAXParserFactory factory = SAXParserFactory.newInstance(); | |
SAXParser saxParser = factory.newSAXParser(); | |
Zad1Handler handler = new Zad1Handler(); | |
saxParser.parse(FILE, handler); | |
System.out.println(handler.statystyki()); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
private static class Zad1Handler extends DefaultHandler { | |
private int zagniezdzenie = 0; | |
private int maxZagniezdzenie = 0; | |
private Map<String, Integer> wystapienia = new HashMap<>(); | |
private Map<Integer, Integer> elementówNaPoziomie = new HashMap<>(); | |
private int maxDlugoscTekstu = 0; | |
private int minDlugoscTekstu = 0; | |
private int maxAtrybutow = 0; | |
private int minAtrybutow = 0; | |
public void startElement(String uri, String localName, String qName, | |
Attributes attributes) throws SAXException { | |
zagniezdzenie++; | |
maxZagniezdzenie = Math.max(zagniezdzenie, maxZagniezdzenie); | |
maxAtrybutow = Math.max(maxAtrybutow, attributes.getLength()); | |
minAtrybutow = Math.min(minAtrybutow, attributes.getLength()); | |
increaseStatistic(wystapienia, qName); | |
increaseStatistic(elementówNaPoziomie, zagniezdzenie); | |
printTabs(); | |
System.out.println(qName); | |
} | |
private <T> void increaseStatistic(Map<T, Integer> map, T key) { | |
if (map.containsKey(key)) { | |
map.put(key, map.get(key) + 1); | |
} else { | |
map.put(key, 1); | |
} | |
} | |
public void endElement(String uri, String localName, | |
String qName) throws SAXException { | |
zagniezdzenie--; | |
} | |
private void printTabs() { | |
for (int i = 0; i < zagniezdzenie; ++i) { | |
System.out.print(" "); | |
} | |
} | |
public void characters(char ch[], int start, int length) throws SAXException { | |
minDlugoscTekstu = Math.min(minDlugoscTekstu, length); | |
maxDlugoscTekstu = Math.max(maxDlugoscTekstu, length); | |
} | |
public Map<String, String> statystyki() { | |
Map<String, String> map = new HashMap<>(); | |
map.put("Głębokość", Integer.toString(maxZagniezdzenie)); | |
map.put("Wystąpienia", wystapienia.toString()); | |
map.put("Elementow na poziomie", elementówNaPoziomie.toString()); | |
map.put("Maks dlugosc tesktu", Integer.toString(maxDlugoscTekstu)); | |
map.put("Min dlugosc tesktu", Integer.toString(minDlugoscTekstu)); | |
map.put("Max atrybutow", Integer.toString(maxAtrybutow)); | |
map.put("Min atrybutow", Integer.toString(minAtrybutow)); | |
return map; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.xml.parsers.DocumentBuilderFactory; | |
import javax.xml.parsers.DocumentBuilder; | |
import javax.xml.parsers.ParserConfigurationException; | |
import org.w3c.dom.Document; | |
import org.w3c.dom.NodeList; | |
import org.w3c.dom.Node; | |
import org.w3c.dom.Element; | |
import org.xml.sax.SAXException; | |
import java.io.File; | |
import java.io.IOException; | |
public class Zad2 { | |
private static final String FILE = "C:\\Documents and Settings\\Administrator\\Pulpit\\mkyong.xml"; | |
//private static final String FILE = "C:\\Documents and Settings\\Administrator\\Pulpit\\sredniowiecze.xml"; | |
public static void main(String argv[]) { | |
try { | |
Document doc = readDocument(FILE); | |
Element documentElement = doc.getDocumentElement(); | |
printNode(documentElement, 0); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
public static void printNode(Node n, int zagniezdzenie) { | |
tab(zagniezdzenie); | |
System.out.printf("<%s>%n", n.getNodeName()); | |
NodeList childNodes = n.getChildNodes(); | |
for (int i = 0; i < childNodes.getLength(); i++) { | |
Node node = childNodes.item(i); | |
if (node.getNodeType() == Node.ELEMENT_NODE) { | |
printNode(node, zagniezdzenie + 1); | |
} else if (node.getNodeType() == Node.TEXT_NODE && !node.getNodeValue().trim().equals("")) { | |
tab(zagniezdzenie + 1); | |
System.out.printf("'%s'%n", node.getNodeValue().trim()); | |
} | |
} | |
} | |
private static void tab(int zagniezdzenie) { | |
for (int i = 0; i < zagniezdzenie; i++) { | |
System.out.print(" "); | |
} | |
} | |
static Document readDocument(String file) throws ParserConfigurationException, SAXException, IOException { | |
File fXmlFile = new File(file); | |
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); | |
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); | |
Document doc = dBuilder.parse(fXmlFile); | |
doc.getDocumentElement().normalize(); | |
return doc; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.w3c.dom.Document; | |
import org.w3c.dom.Element; | |
public class Zad22 { | |
private static final String FILE = "C:\\Documents and Settings\\Administrator\\Pulpit\\mkyong.xml"; | |
public static void main(String argv[]) { | |
try { | |
Document doc = Zad2.readDocument(FILE); | |
Element staff = getFirstChildByTag(doc.getDocumentElement(), "staff"); | |
dodajWezel(staff, "firstname", doc); | |
zmienWezel(staff, "lastname"); | |
usunWezel(staff, "nickname"); | |
Zad2.printNode(doc.getDocumentElement(), 0); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
private static void dodajWezel(Element parent, String nazwaWezla, Document doc) { | |
Element element = getFirstChildByTag(parent, nazwaWezla); | |
Element nowyWezel = doc.createElement("nowyWezel"); | |
element.appendChild(nowyWezel); | |
} | |
private static void zmienWezel(Element parent, String nazwaWezla) { | |
Element element = getFirstChildByTag(parent, nazwaWezla); | |
element.setTextContent("zmieniony wezel"); | |
} | |
private static void usunWezel(Element parent, String nazwaWezla) { | |
Element element = getFirstChildByTag(parent, nazwaWezla); | |
parent.removeChild(element); | |
} | |
private static Element getFirstChildByTag(Element documentElement, String tagName) { | |
return (Element) documentElement.getElementsByTagName(tagName).item(0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment