In the following examples, we showcase how DomUtils, a utility class in the commons-xml project, can be utilized to simplify XML processing tasks. DomUtils provides convenient methods and functionalities for tasks such as parsing XML, manipulating nodes, validating documents, and extracting information using XPath expressions. Through these code snippets, you will gain insights into how to effectively use DomUtils to insert elements, copy attributes and children, rename nodes, convert nodes to XML strings, and more.
import com.dataliquid.commons.xml.DomUtils;
String xmlString = "<root><element>Value</element></root>";
Document document = DomUtils.parse(xmlString);
// You can further process the parsed Document object here
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import com.dataliquid.commons.xml.DomUtils;
// Assume we have a parent Node (parentNode) and an element to insert (elementToInsert)
Node parentNode = ...; // Your parent Node
Element elementToInsert = ...; // The element to insert
// Assume we have a List of ordered node names
List<String> orderedNodeNames = Arrays.asList("node1", "node2", "node3");
Element insertedElement = DomUtils.insertElement(parentNode, elementToInsert, orderedNodeNames);
// The element is inserted into the parent node according to the ordered node names
// You can further process the insertedElement here
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import com.dataliquid.commons.xml.DomUtils;
// Assume we have a reference Node and an Element to insert
Node referenceNode = ...; // Your reference Node
Element elementToInsert = ...; // The Element to insert
Element insertedElement = DomUtils.insertElementBefore(referenceNode, elementToInsert);
// The method inserts the Element before the reference Node and returns the inserted Element
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import com.dataliquid.commons.xml.DomUtils;
// Assume we have a reference Node and an Element to insert
Node referenceNode = ...; // Your reference Node
Element elementToInsert = ...; // The Element to insert
Element insertedElement = DomUtils.insertElementAfter(referenceNode, elementToInsert);
// The method inserts the Element after the reference Node and returns the inserted Element
import org.w3c.dom.Node;
import com.dataliquid.commons.xml.DomUtils;
// Assume we have a List of nodes to delete
List<Node> nodesToDelete = ...; // Your List of nodes to delete
DomUtils.delete(nodesToDelete);
// The nodes in the list are deleted from their parent nodes
import org.w3c.dom.Node;
import com.dataliquid.commons.xml.DomUtils;
// Assume we have a node and an XPath expression
Node node = ...; // Your node
String xpath = "..."; // Your XPath expression
String selectedString = DomUtils.selectString(node, xpath);
// The method selects the string value based on the provided XPath expression
// You can further process the selectedString here
import org.w3c.dom.Node;
import com.dataliquid.commons.xml.DomUtils;
// Assume we have a parent node
Node parentNode = ...; // Your parent node
List<Node> childNodes = DomUtils.selectChildNodes(parentNode);
// The method selects and returns a list of child nodes of the parent node
// You can further process the childNodes list here
import org.w3c.dom.Element;
import com.dataliquid.commons.xml.DomUtils;
// Assume we have a source element and a destination element
Element sourceElement = ...; // Your source element
Element destinationElement = ...; // Your destination element
DomUtils.copyAttributes(sourceElement, destinationElement);
// The method copies the attributes from the source element to the destination element
import org.w3c.dom.Element;
import com.dataliquid.commons.xml.DomUtils;
// Assume we have a source element and a destination element
Element sourceElement = ...; // Your source element
Element destinationElement = ...; // Your destination element
DomUtils.copyChildren(sourceElement, destinationElement);
// The method copies the children nodes from the source element to the destination element
import org.w3c.dom.Node;
import com.dataliquid.commons.xml.DomUtils;
// Assume we have a node and a new name
Node node = ...; // Your node
String newName = "..."; // The new name for the node
Node renamedNode = DomUtils.renameNode(node, newName);
// The method renames the node with the new name and returns the renamed node
import org.w3c.dom.Document;
import javax.xml.validation.Schema;
import com.dataliquid.commons.xml.DomUtils;
// Assume we have a Document and a Schema
Document document = ...; // Your Document to validate
Schema schema = ...; // Your Schema for validation
boolean isValid = DomUtils.validate(document, schema);
// The method validates the Document against the provided Schema and returns true if it's valid
if (isValid) {
// Document is valid
// You can further process the validated document here
} else {
// Document is invalid
// Handle the validation failure
}
import org.w3c.dom.Node;
import com.dataliquid.commons.xml.DomUtils;
// Assume we have a Node and a flag for indentation
Node node = ...; // Your Node
boolean indent = true; // Whether to indent the XML output
String xmlString = DomUtils.asXml(node, indent);
// The method converts the Node to its XML representation as a String
// You can further process the xmlString here