CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-cn-hutool--hutool-core

Comprehensive Java utility library providing collections, strings, beans, dates, I/O, and numerous other utility functions.

Pending
Overview
Eval results
Files

xml-operations.mddocs/

XML Operations

XML parsing, manipulation, and serialization utilities through the XmlUtil class.

Capabilities

XML Document Reading

Parse XML from various sources into DOM Document objects.

/**
 * Read XML document from file
 * @param file XML file to read
 * @return parsed Document object
 */
public static Document readXML(File file);

/**
 * Read XML from string (file path or XML content)
 * @param pathOrContent file path or XML content string
 * @return parsed Document object
 */
public static Document readXML(String pathOrContent);

/**
 * Read XML document from input stream
 * @param inputStream XML input stream
 * @return parsed Document object
 */
public static Document readXML(InputStream inputStream);

/**
 * Read XML document from reader
 * @param reader XML character reader
 * @return parsed Document object
 */
public static Document readXML(Reader reader);

/**
 * Read XML document from InputSource
 * @param source XML input source
 * @return parsed Document object
 */
public static Document readXML(InputSource source);

Usage Examples:

import cn.hutool.core.util.XmlUtil;
import org.w3c.dom.Document;
import java.io.File;

// Read from file
Document doc = XmlUtil.readXML(new File("config.xml"));

// Read from XML string
String xmlContent = "<root><item>value</item></root>";
Document docFromString = XmlUtil.readXML(xmlContent);

// Read from classpath file
Document docFromPath = XmlUtil.readXML("/config/app.xml");

SAX Parsing

Parse XML using SAX parser with custom content handlers.

/**
 * Read XML using SAX parser from file
 * @param file XML file to read
 * @param contentHandler SAX content handler
 */
public static void readBySax(File file, ContentHandler contentHandler);

/**
 * Read XML using SAX parser from reader
 * @param reader XML character reader
 * @param contentHandler SAX content handler
 */
public static void readBySax(Reader reader, ContentHandler contentHandler);

/**
 * Read XML using SAX parser from input stream
 * @param source XML input stream
 * @param contentHandler SAX content handler
 */
public static void readBySax(InputStream source, ContentHandler contentHandler);

XML Serialization

Convert DOM nodes and documents to string representations.

/**
 * Convert XML node to string
 * @param doc XML node to convert
 * @return XML string representation
 */
public static String toStr(Node doc);

/**
 * Convert XML document to string
 * @param doc XML document to convert
 * @return XML string representation
 */
public static String toStr(Document doc);

/**
 * Convert XML node to formatted string
 * @param doc XML node to convert
 * @param isPretty whether to format with indentation
 * @return XML string representation
 */
public static String toStr(Node doc, boolean isPretty);

/**
 * Convert XML node to string with charset and formatting
 * @param doc XML node to convert
 * @param charset character encoding
 * @param isPretty whether to format with indentation
 * @return XML string representation
 */
public static String toStr(Node doc, String charset, boolean isPretty);

/**
 * Convert XML node to string with full options
 * @param doc XML node to convert
 * @param charset character encoding
 * @param isPretty whether to format with indentation
 * @param omitXmlDeclaration whether to omit XML declaration
 * @return XML string representation
 */
public static String toStr(Node doc, String charset, boolean isPretty, boolean omitXmlDeclaration);

Usage Examples:

import cn.hutool.core.util.XmlUtil;
import org.w3c.dom.Document;

Document doc = XmlUtil.readXML("<root><item>value</item></root>");

// Basic string conversion
String xmlString = XmlUtil.toStr(doc); // Compact XML

// Pretty formatted XML
String prettyXml = XmlUtil.toStr(doc, true);

// Custom charset and formatting
String customXml = XmlUtil.toStr(doc, "UTF-8", true, false);

XML Writing

Write XML documents to files and streams.

/**
 * Write XML node to output stream
 * @param node XML node to write
 * @param out output stream
 * @param charset character encoding
 * @param indent indentation size
 */
public static void write(Node node, OutputStream out, String charset, int indent);

/**
 * Write XML node to writer
 * @param node XML node to write
 * @param writer character writer
 * @param charset character encoding
 * @param indent indentation size
 */
public static void write(Node node, Writer writer, String charset, int indent);

/**
 * Write XML node with declaration control
 * @param node XML node to write
 * @param out output stream
 * @param charset character encoding
 * @param indent indentation size
 * @param omitXmlDeclaration whether to omit XML declaration
 */
public static void write(Node node, OutputStream out, String charset, int indent, boolean omitXmlDeclaration);

XPath Operations

Execute XPath expressions on XML documents.

/**
 * Get element by XPath expression
 * @param doc XML document
 * @param expression XPath expression
 * @return matching element or null
 */
public static Element getElementByXPath(Document doc, String expression);

/**
 * Get elements by XPath expression
 * @param doc XML document
 * @param expression XPath expression
 * @return NodeList of matching elements
 */
public static NodeList getElementListByXPath(Document doc, String expression);

/**
 * Get object by XPath expression
 * @param doc XML document
 * @param expression XPath expression
 * @param returnType expected return type
 * @return XPath evaluation result
 */
public static Object getByXPath(Document doc, String expression, QName returnType);

Usage Examples:

import cn.hutool.core.util.XmlUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

String xml = "<root><users><user id='1'>Alice</user><user id='2'>Bob</user></users></root>";
Document doc = XmlUtil.readXML(xml);

// XPath queries
Element userElement = XmlUtil.getElementByXPath(doc, "//user[@id='1']");
NodeList allUsers = XmlUtil.getElementListByXPath(doc, "//user");
String userName = (String) XmlUtil.getByXPath(doc, "//user[@id='1']/text()", XPathConstants.STRING);

XML Entity Operations

Handle XML entity encoding and decoding.

/**
 * Escape XML special characters
 * @param string string to escape
 * @return escaped XML string
 */
public static String escape(String string);

/**
 * Unescape XML entities to original characters
 * @param string string with XML entities
 * @return unescaped string
 */
public static String unescape(String string);

/**
 * Remove invalid XML characters
 * @param string string to clean
 * @return string with invalid XML characters removed
 */
public static String cleanInvalid(String string);

Usage Examples:

import cn.hutool.core.util.XmlUtil;

// Entity handling
String original = "Hello <world> & \"quotes\"";
String escaped = XmlUtil.escape(original); // "Hello &lt;world&gt; &amp; &quot;quotes&quot;"
String unescaped = XmlUtil.unescape(escaped); // "Hello <world> & \"quotes\""

// Clean invalid characters
String withInvalid = "Hello\u0001World\u0008"; 
String cleaned = XmlUtil.cleanInvalid(withInvalid); // "HelloWorld"

Document Creation

Create new XML documents and elements.

/**
 * Create new XML document
 * @return new empty Document
 */
public static Document createXml();

/**
 * Create document with root element
 * @param rootElementName root element name
 * @return new Document with root element
 */
public static Document createXml(String rootElementName);

/**
 * Get document builder factory
 * @return DocumentBuilderFactory instance
 */
public static DocumentBuilderFactory getDocumentBuilderFactory();

/**
 * Get document builder
 * @return DocumentBuilder instance
 */
public static DocumentBuilder getDocumentBuilder();

Common Types

// XML processing types
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import javax.xml.namespace.QName;
import javax.xml.xpath.XPathConstants;

// XML entity constants
public static final String NBSP = "&nbsp;";
public static final String AMP = "&amp;";
public static final String QUOTE = "&quot;";
public static final String APOS = "&apos;";
public static final String LT = "&lt;";
public static final String GT = "&gt;";

Install with Tessl CLI

npx tessl i tessl/maven-cn-hutool--hutool-core

docs

collection-operations.md

compression-operations.md

date-time-operations.md

file-io-operations.md

index.md

object-operations.md

random-operations.md

string-operations.md

type-conversion.md

url-operations.md

xml-operations.md

tile.json