List of usage examples for org.w3c.dom Element getTagName
public String getTagName();
From source file:org.opendatakit.aggregate.parser.SubmissionParser.java
/** * Recursive function that prints the nodes from an XML tree * //w ww . j a v a 2s .com * @param node * xml node to be recursively printed */ @SuppressWarnings("unused") private void printNode(Element node) { System.out.println(ParserConsts.NODE_FORMATTED + node.getTagName()); if (node.hasAttributes()) { NamedNodeMap attributes = node.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { Node attr = attributes.item(i); System.out.println(ParserConsts.ATTRIBUTE_FORMATTED + attr.getNodeName() + BasicConsts.EQUALS + attr.getNodeValue()); } } if (node.hasChildNodes()) { NodeList children = node.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if (child.getNodeType() == Node.ELEMENT_NODE) { printNode((Element) child); } else if (child.getNodeType() == Node.TEXT_NODE) { String value = child.getNodeValue().trim(); if (value.length() > 0) { System.out.println(ParserConsts.VALUE_FORMATTED + value); } } } } }
From source file:org.openecomp.sdnc.uebclient.SdncUebCallback.java
private SdncArtifactType analyzeFileType(ArtifactTypeEnum artifactType, File spoolFile, Document spoolDoc) { if (artifactType != ArtifactTypeEnum.YANG_XML) { LOG.error("Unexpected artifact type - expecting YANG_XML, got " + artifactType); return (null); }/*from w w w. j a v a 2 s . c o m*/ // Examine outer tag try { Element root = spoolDoc.getDocumentElement(); String rootName = root.getTagName(); if (rootName.contains(":")) { String[] rootNameElems = rootName.split(":"); rootName = rootNameElems[rootNameElems.length - 1]; } if (rootName != null) { SdncArtifactType mapEntry = config.getMapping(rootName); if (mapEntry == null) { LOG.error("Unexpected file contents - root tag is " + rootName); } return (mapEntry); } else { LOG.error("Cannot get root tag from file"); return (null); } } catch (Exception e) { LOG.error("Could not parse YANG_XML file " + spoolFile.getName(), e); return (null); } }
From source file:org.openestate.io.core.XmlUtils.java
private static void printNode(Element node, int indent) { String prefix = (indent > 0) ? StringUtils.repeat(">", indent) + " " : ""; LOGGER.debug(prefix + "<" + node.getTagName() + "> / " + node.getNamespaceURI() + " / " + node.getPrefix()); prefix = StringUtils.repeat(">", indent + 1) + " "; NamedNodeMap attribs = node.getAttributes(); for (int i = 0; i < attribs.getLength(); i++) { Attr attrib = (Attr) attribs.item(i); LOGGER.debug(prefix + "@" + attrib.getName() + " / " + attrib.getNamespaceURI() + " / " + attrib.getPrefix());/*from w w w . j a v a 2 s .c o m*/ } NodeList children = node.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if (child instanceof Element) { XmlUtils.printNode((Element) child, indent + 1); } } }
From source file:org.openestate.io.openimmo.converters.OpenImmo_1_2_0.java
/** * Downgrade <mieteinnahmen_ist>, <mieteinnahmen_soll> elements * to OpenImmo 1.1.//from w ww.ja v a 2 s . c o m * <p> * The "periode" attribute of the <mieteinnahmen_ist> and * <mieteinnahmen_soll> elements is not available in version 1.1. * <p> * Any occurences of these values is removed. * <p> * The numeric value within the <mieteinnahmen_ist> and * <mieteinnahmen_soll> elements is converted according to the value of * the "periode" attribute. * * @param doc OpenImmo document in version 1.2.0 * @throws JaxenException */ protected void downgradeMieteinnahmenElements(Document doc) throws JaxenException { List nodes = XmlUtils .newXPath( "/io:openimmo/io:anbieter/io:immobilie/io:preise/io:mieteinnahmen_ist[@periode] |" + "/io:openimmo/io:anbieter/io:immobilie/io:preise/io:mieteinnahmen_soll[@periode]", doc) .selectNodes(doc); for (Object item : nodes) { Element node = (Element) item; String value = StringUtils.trimToNull(node.getTextContent()); Double numericValue = null; try { numericValue = (value != null) ? DatatypeConverter.parseDouble(value) : null; } catch (Exception ex) { String tagName = node.getTagName(); LOGGER.warn("Can't parse <" + tagName + ">" + value + "</" + tagName + "> as number!"); LOGGER.warn("> " + ex.getLocalizedMessage(), ex); } if (numericValue != null && numericValue > 0) { String periode = StringUtils.trimToNull(node.getAttribute("periode")); if ("MONAT".equalsIgnoreCase(periode)) { node.setTextContent(DatatypeConverter.printDouble(numericValue * 12)); } else if ("WOCHE".equalsIgnoreCase(periode)) { node.setTextContent(DatatypeConverter.printDouble(numericValue * 52)); } else if ("TAG".equalsIgnoreCase(periode)) { node.setTextContent(DatatypeConverter.printDouble(numericValue * 365)); } } node.removeAttribute("periode"); } }
From source file:org.openhab.action.nma.internal.NotifyMyAndroid.java
private static String parseResponse(String response) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder db = factory.newDocumentBuilder(); InputSource inStream = new InputSource(); inStream.setCharacterStream(new StringReader(response)); Document doc = db.parse(inStream); Element root = doc.getDocumentElement(); String lastError = null;/*from w ww . j a v a 2s .c o m*/ if (root.getTagName().equals("nma")) { Node item = root.getFirstChild(); String childName = item.getNodeName(); if (!childName.equals("success")) { lastError = item.getFirstChild().getNodeValue(); } } return lastError; }
From source file:org.openhab.action.pushover.internal.Pushover.java
private static String parseResponse(String response) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder db = factory.newDocumentBuilder(); InputSource inStream = new InputSource(); inStream.setCharacterStream(new StringReader(response)); Document doc = db.parse(inStream); Element root = doc.getDocumentElement(); if (API_RETURN_ROOT_TAG.equals(root.getTagName())) { NodeList statusList = root.getElementsByTagName(API_RETURN_STATUS_TAG); for (int i = 0; i < statusList.getLength(); i++) { Element value = (Element) statusList.item(i); if (API_RETURN_STATUS_SUCCESS.equals(value.getFirstChild().getNodeValue())) { return null; }/*from ww w .j a v a 2s. co m*/ } NodeList errorList = root.getElementsByTagName(API_RETURN_ERROR_TAG); Element value = (Element) errorList.item(0); return value.getFirstChild().getNodeValue(); } return response; }
From source file:org.openhealthtools.openatna.audit.server.ServerConfiguration.java
private boolean processArr(Element parent) { IConnectionDescription tcp = null;/*from w w w. j a v a 2s.c o m*/ IConnectionDescription udp = null; NodeList children = parent.getChildNodes(); int threads = 5; boolean nio = false; for (int i = 0; i < children.getLength(); i++) { Node n = children.item(i); if (n instanceof Element) { Element el = (Element) n; if (el.getTagName().equalsIgnoreCase("TCP")) { String conn = el.getAttribute("connection"); if (conn == null) { throw new RuntimeException("No connection defined for Audit Record Repository"); } tcp = ConnectionFactory.getConnectionDescription(conn); if (tcp == null) { throw new RuntimeException("No connection defined for Audit Record Repository"); } } else if (el.getTagName().equalsIgnoreCase("UDP")) { String conn = el.getAttribute("connection"); if (conn == null) { throw new RuntimeException("No connection defined for Audit Record Repository"); } udp = ConnectionFactory.getConnectionDescription(conn); if (udp == null) { throw new RuntimeException("No connection defined for Audit Record Repository"); } } else if (el.getTagName().equalsIgnoreCase("EXECUTIONTHREADS")) { String t = el.getTextContent().trim(); if (t.length() > 0) { try { threads = Integer.parseInt(t); } catch (NumberFormatException e) { log.warn("Could not parse number of execution threads. Using default"); } if (threads < 1) { threads = 5; } } } else if (el.getTagName().equalsIgnoreCase("NIO")) { String t = el.getTextContent().trim(); if (t.length() > 0) { if (t.equalsIgnoreCase("true") || t.equalsIgnoreCase("1") || t.equalsIgnoreCase("yes")) { nio = true; } } } } } if (tcp != null && udp != null) { AtnaServer server = new AtnaServer(tcp, udp, threads, nio); servers.add(server); return true; } else { log.warn("No connections defined for server. This ARR will be not able to receive Syslog Messages.\n" + "The service will shut down unless it is being run from inside a separate execution thread."); return false; } }
From source file:org.openmrs.module.muzima.handler.XmlEncounterQueueDataHandler.java
private void processPatient(final Encounter encounter, final NodeList patientNodeList) throws QueueProcessorException { Node patientNode = patientNodeList.item(0); NodeList patientElementNodes = patientNode.getChildNodes(); Patient unsavedPatient = new Patient(); PersonName personName = new PersonName(); PatientIdentifier patientIdentifier = new PatientIdentifier(); for (int i = 0; i < patientElementNodes.getLength(); i++) { Node patientElementNode = patientElementNodes.item(i); if (patientElementNode.getNodeType() == Node.ELEMENT_NODE) { Element patientElement = (Element) patientElementNode; if (patientElement.getTagName().equals("patient.middle_name")) { personName.setMiddleName(patientElement.getTextContent()); } else if (patientElement.getTagName().equals("patient.given_name")) { personName.setGivenName(patientElement.getTextContent()); } else if (patientElement.getTagName().equals("patient.family_name")) { personName.setFamilyName(patientElement.getTextContent()); } else if (patientElement.getTagName().equals("patient_identifier.identifier_type_id")) { int identifierTypeId = Integer.parseInt(patientElement.getTextContent()); PatientIdentifierType identifierType = Context.getPatientService() .getPatientIdentifierType(identifierTypeId); patientIdentifier.setIdentifierType(identifierType); } else if (patientElement.getTagName().equals("patient.medical_record_number")) { patientIdentifier.setIdentifier(patientElement.getTextContent()); } else if (patientElement.getTagName().equals("patient.sex")) { unsavedPatient.setGender(patientElement.getTextContent()); } else if (patientElement.getTagName().equals("patient.birthdate")) { Date dob = parseDate(patientElement.getTextContent()); unsavedPatient.setBirthdate(dob); } else if (patientElement.getTagName().equals("patient.uuid")) { unsavedPatient.setUuid(patientElement.getTextContent()); }/*from w w w . j ava 2s .com*/ } } unsavedPatient.addName(personName); unsavedPatient.addIdentifier(patientIdentifier); Patient candidatePatient; if (StringUtils.isNotEmpty(unsavedPatient.getUuid())) { candidatePatient = Context.getPatientService().getPatientByUuid(unsavedPatient.getUuid()); if (candidatePatient == null) { String temporaryUuid = unsavedPatient.getUuid(); RegistrationDataService dataService = Context.getService(RegistrationDataService.class); RegistrationData registrationData = dataService.getRegistrationDataByTemporaryUuid(temporaryUuid); candidatePatient = Context.getPatientService().getPatientByUuid(registrationData.getAssignedUuid()); } } else if (!StringUtils.isBlank(patientIdentifier.getIdentifier())) { List<Patient> patients = Context.getPatientService().getPatients(patientIdentifier.getIdentifier()); candidatePatient = findPatient(patients, unsavedPatient); } else { List<Patient> patients = Context.getPatientService() .getPatients(unsavedPatient.getPersonName().getFullName()); candidatePatient = findPatient(patients, unsavedPatient); } if (candidatePatient == null) { queueProcessorException .addException(new Exception("Unable to uniquely identify patient for this encounter form data. " + ToStringBuilder.reflectionToString(unsavedPatient))); } encounter.setPatient(candidatePatient); }
From source file:org.openmrs.module.muzima.handler.XmlEncounterQueueDataHandler.java
private void processEncounter(final Encounter encounter, final NodeList encounterNodeList) throws QueueProcessorException { Node encounterNode = encounterNodeList.item(0); NodeList encounterElementNodes = encounterNode.getChildNodes(); for (int i = 0; i < encounterElementNodes.getLength(); i++) { Node encounterElementNode = encounterElementNodes.item(i); if (encounterElementNode.getNodeType() == Node.ELEMENT_NODE) { Element encounterElement = (Element) encounterElementNode; String encounterElementValue = encounterElement.getTextContent(); if (encounterElement.getTagName().equals("encounter.encounter_datetime")) { Date date = parseDate(encounterElementValue); encounter.setEncounterDatetime(date); } else if (encounterElement.getTagName().equals("encounter.location_id")) { int locationId = NumberUtils.toInt(encounterElementValue, -999); Location location = Context.getLocationService().getLocation(locationId); if (location == null) { queueProcessorException.addException(new Exception( "Unable to find encounter location using the id: " + encounterElementValue)); }// w w w . j a va 2 s . c o m encounter.setLocation(location); } else if (encounterElement.getTagName().equals("encounter.provider_id")) { User user = Context.getUserService().getUserByUsername(encounterElementValue); if (user == null) { queueProcessorException.addException( new Exception("Unable to find user using the id: " + encounterElementValue)); } encounter.setProvider(user); encounter.setCreator(user); } else if (encounterElement.getTagName().equals("encounter.form_uuid")) { Form form = Context.getFormService().getFormByUuid(encounterElementValue); if (form == null) { MuzimaFormService muzimaFormService = Context.getService(MuzimaFormService.class); MuzimaForm muzimaForm = muzimaFormService.getFormByUuid(encounterElementValue); if (muzimaForm != null) { Form formDefinition = Context.getFormService().getFormByUuid(muzimaForm.getForm()); encounter.setForm(formDefinition); encounter.setEncounterType(formDefinition.getEncounterType()); } else { log.info("Unable to find form using the uuid: " + encounterElementValue + ". Setting the form field to null!"); } } else { encounter.setForm(form); encounter.setEncounterType(form.getEncounterType()); } } else if (encounterElement.getTagName().equals("encounter.encounter_type")) { if (encounter.getEncounterType() == null) { int encounterTypeId = NumberUtils.toInt(encounterElementValue, -999); EncounterType encounterType = Context.getEncounterService() .getEncounterType(encounterTypeId); if (encounterType == null) { queueProcessorException.addException(new Exception( "Unable to find encounter type using the id: " + encounterElementValue)); } encounter.setEncounterType(encounterType); } } } } }
From source file:org.openmrs.module.muzima.handler.XmlRegistrationQueueDataHandler.java
private Patient createPatientFromPayload(final String payload) { Patient unsavedPatient = new Patient(); try {/*from w w w .j a v a 2 s . c om*/ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document document = db.parse(new InputSource(new ByteArrayInputStream(payload.getBytes("utf-8")))); Element element = document.getDocumentElement(); element.normalize(); Node patientNode = document.getElementsByTagName("patient").item(0); NodeList patientElementNodes = patientNode.getChildNodes(); PersonName personName = new PersonName(); PatientIdentifier patientIdentifier = new PatientIdentifier(); for (int i = 0; i < patientElementNodes.getLength(); i++) { Node patientElementNode = patientElementNodes.item(i); if (patientElementNode.getNodeType() == Node.ELEMENT_NODE) { Element patientElement = (Element) patientElementNode; String tagName = patientElement.getTagName(); if (tagName.equals("patient.middle_name")) { personName.setMiddleName(patientElement.getTextContent()); } else if (tagName.equals("patient.given_name")) { personName.setGivenName(patientElement.getTextContent()); } else if (tagName.equals("patient.family_name")) { personName.setFamilyName(patientElement.getTextContent()); } else if (tagName.equals("patient_identifier.identifier_type_id")) { int identifierTypeId = Integer.parseInt(patientElement.getTextContent()); PatientIdentifierType identifierType = Context.getPatientService() .getPatientIdentifierType(identifierTypeId); if (identifierType == null) { queueProcessorException.addException(new Exception( "Unable to find patient identifier type with id: " + identifierTypeId)); } else { patientIdentifier.setIdentifierType(identifierType); } } else if (tagName.equals("patient.medical_record_number")) { patientIdentifier.setIdentifier(patientElement.getTextContent()); } else if (tagName.equals("patient.sex")) { unsavedPatient.setGender(patientElement.getTextContent()); } else if (tagName.equals("patient.birthdate")) { Date dob = parseDate(patientElement.getTextContent()); unsavedPatient.setBirthdate(dob); } else if (tagName.equals("patient.uuid")) { unsavedPatient.setUuid(patientElement.getTextContent()); setTemporaryPatientUuid(patientElement.getTextContent()); } else if (tagName.equals("patient.finger")) { savePatientsFinger(unsavedPatient, patientElement.getTextContent()); } else if (tagName.equals("patient.fingerprint")) { savePatientsFingerprint(unsavedPatient, patientElement.getTextContent()); } else if (tagName.equals("amrs_medical_record_number_identifier_type")) { extractIdentifier(unsavedPatient, patientElement, "AMRS Medical Record Number"); } else if (tagName.equals("ccc_identifier_type")) { extractIdentifier(unsavedPatient, patientElement, "CCC Number "); } else if (tagName.equals("hct_identifier_type")) { extractIdentifier(unsavedPatient, patientElement, "HCT ID"); } else if (tagName.equals("kni_identifier_type")) { extractIdentifier(unsavedPatient, patientElement, "KENYAN NATIONAL ID NUMBER"); } else if (tagName.equals("mtct_identifier_type")) { extractIdentifier(unsavedPatient, patientElement, "MTCT Plus ID"); } else if (tagName.equals("mtrh_hospital_number_identifier_type")) { extractIdentifier(unsavedPatient, patientElement, "MTRH Hospital Number"); } else if (tagName.equals("old_amrs_number_identifier_type")) { extractIdentifier(unsavedPatient, patientElement, "Old AMPATH Medical Record Number"); } else if (tagName.equals("pmtc_identifier_type")) { extractIdentifier(unsavedPatient, patientElement, "pMTCT ID"); } else if (tagName.startsWith("person_attribute")) { PersonService personService = Context.getPersonService(); int personAttributeTypeId = NumberUtils.toInt(tagName.replace("person_attribute", "")); PersonAttributeType personAttributeType = personService .getPersonAttributeType(personAttributeTypeId); if (personAttributeType == null) { queueProcessorException.addException(new Exception( "Unable to find attribute type with id: " + personAttributeTypeId)); } else { PersonAttribute personAttribute = new PersonAttribute(); personAttribute.setAttributeType(personAttributeType); personAttribute.setValue(patientElement.getTextContent()); unsavedPatient.addAttribute(personAttribute); } } } } Node encounterNode = document.getElementsByTagName("encounter").item(0); NodeList encounterElementNodes = encounterNode.getChildNodes(); for (int i = 0; i < encounterElementNodes.getLength(); i++) { Node encounterElementNode = encounterElementNodes.item(i); if (encounterElementNode.getNodeType() == Node.ELEMENT_NODE) { Element encounterElement = (Element) encounterElementNode; if (encounterElement.getTagName().equals("encounter.location_id")) { int locationId = Integer.parseInt(encounterElement.getTextContent()); Location location = Context.getLocationService().getLocation(locationId); if (location == null) { queueProcessorException .addException(new Exception("Unable to find location with id: " + locationId)); } else { patientIdentifier.setLocation(location); } for (PatientIdentifier identifier : unsavedPatient.getIdentifiers()) { identifier.setLocation(location); } } } } unsavedPatient.addName(personName); unsavedPatient.addIdentifier(patientIdentifier); } catch (ParserConfigurationException e) { queueProcessorException.addException(new Exception(e.getMessage())); } catch (SAXException e) { queueProcessorException.addException(new Exception(e.getMessage())); } catch (IOException e) { queueProcessorException.addException(new Exception(e.getMessage())); } return unsavedPatient; }