Java tutorial
//package com.java2s; /* ==================================================================== Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ==================================================================== */ import org.w3c.dom.Attr; import org.w3c.dom.Element; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; public class Main { static boolean canBeMerged(Node node1, Node node2, String requiredTagName) { if (node1.getNodeType() != Node.ELEMENT_NODE || node2.getNodeType() != Node.ELEMENT_NODE) return false; Element element1 = (Element) node1; Element element2 = (Element) node2; if (!equals(requiredTagName, element1.getTagName()) || !equals(requiredTagName, element2.getTagName())) return false; NamedNodeMap attributes1 = element1.getAttributes(); NamedNodeMap attributes2 = element2.getAttributes(); if (attributes1.getLength() != attributes2.getLength()) return false; for (int i = 0; i < attributes1.getLength(); i++) { final Attr attr1 = (Attr) attributes1.item(i); final Attr attr2; if (isNotEmpty(attr1.getNamespaceURI())) attr2 = (Attr) attributes2.getNamedItemNS(attr1.getNamespaceURI(), attr1.getLocalName()); else attr2 = (Attr) attributes2.getNamedItem(attr1.getName()); if (attr2 == null || !equals(attr1.getTextContent(), attr2.getTextContent())) return false; } return true; } static boolean equals(String str1, String str2) { return str1 == null ? str2 == null : str1.equals(str2); } static boolean isNotEmpty(String str) { return !isEmpty(str); } static boolean isEmpty(String str) { return str == null || str.length() == 0; } }