Last active
August 29, 2015 14:26
-
-
Save mh-github/d1375512abd3ab89f389 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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