XMLTools

Die Klasse XMLTools stellt statische Hilfsmethoden Methoden zur Verfügung, welche immer wieder benötigt werden, um ein XML Dokument zu erstellen.

class Warenkorb


package de.snowbits.tools;

import static de.snowbits.tools.XMLTools.attr;
import static de.snowbits.tools.XMLTools.concat;
import static de.snowbits.tools.XMLTools.createDoc;
import static de.snowbits.tools.XMLTools.elm;
import static de.snowbits.tools.XMLTools.elmConcat;
import static de.snowbits.tools.XMLTools.elmRoot;
import static de.snowbits.tools.XMLTools.makeToString;

import java.util.Date;

import org.w3c.dom.Document;
import org.w3c.dom.Element;


public class Warenkorb {

	private enum Artikelart { Obst, Gemuese; }
	private Document _doc;
	private Element _root;
	private Element _artikelliste;

	public Warenkorb(String vorname, String nachname) throws Exception {
		_doc = createDoc();
		_root = elmRoot(_doc, "Warenkorb", attr("version", "1.0"), attr("datum", new Date()));
		_artikelliste = elm(_doc, "Artikelliste");
		
		Element kunde = elmConcat(_doc, "Kunde", 
					elm(_doc, "Vorname", vorname), elm(_doc, "Nachname", nachname));
		concat(_root, kunde, _artikelliste);
	}
	
	public static void main(String[] args) throws Exception {
		Warenkorb warenkorb = new Warenkorb("Max", "Mustermann");
		warenkorb.addObst("Apfel", 115, 1.65);
		warenkorb.addObst("Birne", 223, 1.75);
		warenkorb.addObst("Pflaume", 456, 0.44);
		warenkorb.addGemuese("Gurke", 568, 0.95);
		warenkorb.addGemuese("Kartoffel", 897, 2.55);		
		
		System.out.println(warenkorb.print());
	}
	
	public String print() throws Exception {
		return makeToString(_doc, null);
	}
	
	public void addObst(String artikelname, Integer artikelNr, Double preis) throws Exception {
		addArtikel(Artikelart.Obst, artikelname, artikelNr, preis);
	}
	
	public void addGemuese(String artikelname, Integer artikelNr, Double preis) throws Exception {
		addArtikel(Artikelart.Gemuese, artikelname, artikelNr, preis);
	}
	
	private void addArtikel(Artikelart artikelart, String artikelname, Integer artikelNr, Double preis) throws Exception {
		concat(_artikelliste, 
				elm(_doc, "Artikel", artikelname, 
					attr("art", artikelart.toString()),
					attr("nr", String.valueOf(artikelNr)), 
					attr("preis", String.valueOf(preis))));
	}
	
}
	

Ausgabe warenkorb.xml


<?xml version="1.0" encoding="UTF-8"?>
<Warenkorb datum="12.07.2010 18.51.29" version="1.0">
  <Kunde>
    <Vorname>Max</Vorname>
    <Nachname>Mustermann</Nachname>
  </Kunde>
  <Artikelliste>
    <Artikel art="Obst" nr="115" preis="1.65">Apfel</Artikel>
    <Artikel art="Obst" nr="223" preis="1.75">Birne</Artikel>
    <Artikel art="Obst" nr="456" preis="0.44">Pflaume</Artikel>
    <Artikel art="Gemuese" nr="568" preis="0.95">Gurke</Artikel>
    <Artikel art="Gemuese" nr="897" preis="2.55">Kartoffel</Artikel>
  </Artikelliste>
</Warenkorb>
	

class XMLTools


package de.snowbits.tools;

import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class XMLTools {

	private XMLTools() {
		
	}
	
	public static final Document createDoc() throws ParserConfigurationException {
		DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
		Document doc = docBuilder.newDocument();
		
		return doc;
	}		
	
	public static final Element elmRoot(Document doc, String elmName, Attribute... attribute) {
		Element result = elm(doc, elmName);
		
		for (Attribute element : attribute) {
			result.setAttribute(element.getName(), element.getValue());	
		}		
		
		doc.appendChild(result);	
		
		return result;
	}	
	
	public static final Element elm(Document doc, String elmName, Attribute... attribute) {
		Element result = elm(doc, elmName);
		
		for (Attribute element : attribute) {
			result.setAttribute(element.getName(), element.getValue());	
		}		
		
		return result;
	}
	
	public static final void concat(Element elmPrimary, Element... element) {
		for (Element item : element) {
			elmPrimary.appendChild(item);
		}
	}		
	
	public static final Element elmConcat(Document doc, String elmPrimaryName, Element... element) {
		Element result = doc.createElement(elmPrimaryName);
		concat(result, element);
		
		return result;
	}	

	public static final Element elm(Document doc, String elmentName) {
		return doc.createElement(elmentName);
	}	
	
	public static final Element elm(Document doc, String elmentName, String textContent) {
		Element result = doc.createElement(elmentName);
		result.setTextContent(textContent);
		
		return result;
	}	

	public static final Element elm(Document doc, String elmentName, String textContent, Attribute... attribute) {
		Element result = doc.createElement(elmentName);
		result.setTextContent(textContent);
		
		for (Attribute element : attribute) {
			result.setAttribute(element.getName(), element.getValue());	
		}
		
		return result;
	}		
	
	public static final String makeToString(Document doc, String encoding) 
			throws TransformerConfigurationException, TransformerFactoryConfigurationError, TransformerException {
		Transformer transformer = TransformerFactory.newInstance().newTransformer();
		transformer.setOutputProperty(OutputKeys.INDENT,"yes");
		transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,"yes");
		transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount","2");
		StreamResult result = new StreamResult(new StringWriter());
		DOMSource source = new DOMSource(doc);
		transformer.transform(source,result);
		String xmlString=result.getWriter().toString();
		
		return "<?xml version=\"1.0\" encoding=\"" + 
		       (encoding == null ? "UTF-8" : encoding) + "\"?>\n" + xmlString;
	} 		
	
	public static final Attribute attr(String name, String value) throws ParserConfigurationException {
		return new de.snowbits.tools.XMLTools().new Attribute(name, value);
	}	
		
	public static final Attribute attr(String name, Date date) throws ParserConfigurationException {
		return new de.snowbits.tools.XMLTools().new Attribute(name, new SimpleDateFormat("dd.MM.yyyy hh.mm.ss").format(date));
	}	
	
	public class Attribute {
		
		private String _name;
		private String _value;
		
		public Attribute(String name, String value) {
			_name = name;
			_value = value;
		}

		public String getName() {
			return _name;
		}

		public String getValue() {
			return _value;
		}
		
	}
}
	

Top


Example
Sitemap Kontakt Impressum