// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.IOException;
import java.io.StringReader;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.jdom.xpath.XPath;

public class JDomXPath {
    private static final String template = "<protocol>" +
                                           "<type></type>" +
                                           "<version></version>" +
                                           "<commands></commands>" +
                                           "</protocol>";
    public static void fillout(Document doc, String tag, String value) throws JDOMException {
        Element elm = (Element) XPath.selectSingleNode(doc, "protocol/" + tag);
        elm.setText(value);
    }
    public static void main(String[] args) throws JDOMException, IOException {
        SAXBuilder b = new SAXBuilder();
        Document doc = b.build(new StringReader(template));
        fillout(doc, "type", "Dette er en type");
        fillout(doc, "version", "Dette er en version");
        fillout(doc, "commands", "Dette er en commands");
        XMLOutputter fmt = new XMLOutputter(Format.getPrettyFormat());
        System.out.println(fmt.outputString(doc));
    }
}
