Skip to content

Instantly share code, notes, and snippets.

@mh-github
Last active August 29, 2015 14:26
Show Gist options
  • Select an option

  • Save mh-github/d1375512abd3ab89f389 to your computer and use it in GitHub Desktop.

Select an option

Save mh-github/d1375512abd3ab89f389 to your computer and use it in GitHub Desktop.
String filename = "customer.xml";
File file = new File(filename);
SAXBuilder jdomBuilder = new SAXBuilder();
Document jdomDocument = jdomBuilder.build(file);
XPathExpression<Element> expr;
List<Element> elements;
Element element;
XPathFactory xFactory = XPathFactory.instance();
// fetch first and last name
expr = xFactory.compile("/mh/customer/contact/name", Filters.element());
elements = expr.evaluate(jdomDocument);
for (Element elmnt : elements) {
System.out.println(elmnt.getAttributeValue("part") + " " + elmnt.getValue());
String name = elmnt.getValue();
switch(elmnt.getAttributeValue("part")) {
case "first":
customer.setFirstName(name);
break;
case "last":
customer.setLastName(name);
}
}
// fetch email
expr = xFactory.compile("/mh/customer/contact/email", Filters.element());
element = expr.evaluateFirst(jdomDocument);
customer.setEmail(element.getValue());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment