AgentNode extends DOMFragmentReader<List<Agent>> { @Override public List<Agent> read() { List<Agent> agentList = new ArrayList<Agent>(); List<Node> agents = document.selectNodes(FRAGMENT_PATH); int interationSquence = 0; for (Node node : agents) { interationSquence++; AddressBookNode addressBook = new AddressBookNode(node); Node sequenceN = node.selectSingleNode("@sequence"); String sequence = sequenceN != null ? sequenceN.getText() : Integer.toString(interationSquence); Node repTypeN = node.selectSingleNode("@rep-type"); String repType = repTypeN != null ? repTypeN.getText() : null; AgentRepType agentRepType; if (repType != null) { agentRepType = AgentRepType.valueOf(repType.trim().toUpperCase()); } else { agentRepType = AgentRepType.UNDEFINED; } Name name = addressBook.getName(); if (name != null) { Address address = addressBook.getAddress(); Agent agent = new Agent(name, address, agentRepType); agent.setSequence(sequence); if (addressBook.getOrgName() != null) { agent.addRelationship(addressBook.getOrgName(), RelationshipType.REPRESENTATIVE); } agentList.add(agent); } } if (agentList.size() > 0) { return agentList; } List<Node> correspondenceNodes = document.selectNodes(FRAGMENT_PATH2); for (Node node : correspondenceNodes) { AddressBookNode addressBook = new AddressBookNode(node); Name name; if (addressBook.getPersonName() != null) { name = addressBook.getPersonName(); } else { name = addressBook.getOrgName(); } if (name != null) { Address address = addressBook.getAddress(); try { address.validate(); } catch (InvalidDataException e) { LOGGER.warn("{} : {}", e.getMessage(), node.asXML()); } Agent agent = new Agent(name, address, AgentRepType.AGENT); if (addressBook.getOrgName() != null) { agent.addRelationship(addressBook.getOrgName(), RelationshipType.REPRESENTATIVE); } agentList.add(agent); } } return agentList; } AgentNode(Document document); @Override List<Agent> read();  }
@Test public void Agents() throws DocumentException { String xml ="<us-parties><agents>\r\n" + "<agent rep-type=\"attorney\" sequence=\"01\">\r\n" + "<addressbook>\r\n" + "<last-name>Smith</last-name>\r\n" + "<first-name>Jane W.</first-name>\r\n" + "<address>\r\n" + "<country>unknown</country>\r\n" + "</address>\r\n" + "</addressbook>\r\n" + "</agent>\r\n" + "<agent rep-type=\"attorney\" sequence=\"02\">\r\n" + "<addressbook>\r\n" + "<orgname>Doe, John LLC</orgname>\r\n" + "<address>\r\n" + "<country>unknown</country>\r\n" + "</address>\r\n" + "</addressbook>\r\n" + "</agent>\r\n" + "</agents></us-parties>\r\n"; Document doc = DocumentHelper.parseText(xml); List<Agent> agents = new AgentNode(doc).read(); assertEquals("Smith", ((NamePerson) agents.get(0).getName()).getLastName()); assertEquals("Jane W.", ((NamePerson) agents.get(0).getName()).getFirstName()); assertEquals(AgentRepType.ATTORNEY, agents.get(0).getRepType()); assertEquals("Doe, John LLC", ((NameOrg) agents.get(1).getName()).getName()); assertEquals(AgentRepType.ATTORNEY, agents.get(1).getRepType()); }