// 将a.xml元素复制到b.xml
XPath xpath = XPathFactory.newInstance().newXPath();
Node h1NodeFromHtml2 = (Node) xpath.evaluate("//h1", doc2AsDom, XPathConstants.NODE);
Node bodyNodeInHtml1 = (Node) xpath.evaluate("//body", doc1AsDom, XPathConstants.NODE);
Node clonedH1Node = doc1AsDom.importNode(h1NodeFromHtml2, true); // 克隆节点
bodyNodeInHtml1.appendChild(clonedH1Node); // 将克隆的节点添加到html1的body元素下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE root [<!ENTITY INC-C-AVA SYSTEM 'INC-C-AVA.JPG' NDATA JPG>]>
<root>
<element1 attribute1="value1">Text content</element1>
<entity name="INC-C-AVA" path="INC-C-AVA.JPG" width="500" height="600"/>
</root>
try {
StringBuilder doctypeBuilder = new StringBuilder();
List<String> lines = FileUtils.readLines(new File(FILE));
lines.forEach(item->{
if (item.trim().startsWith(")) {
doctypeBuilder.append(item).append("\n");
}
});
// 创建一个DocumentBuilder对象并解析XML文件
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(new File(""));
// 将Document转换为XML字符串
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
//no保留yes去除
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
DOMSource source = new DOMSource(doc);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
transformer.transform(source, result);
String xmlString = writer.toString();
// 在XML字符串前添加DOCTYPE声明
String doctype = doctypeBuilder.toString();
if (!doctype.isEmpty()) {
int startOfRootElement = xmlString.indexOf('>');
int endOfXmlDeclaration = xmlString.indexOf("?>");
xmlString = xmlString.substring(0, endOfXmlDeclaration + 2) + "\n" + doctype + xmlString.substring(startOfRootElement);
}
System.out.println(xmlString);
} catch (Exception e) {
e.printStackTrace();
}
/child/child::text()
/School/Major/Teacher/Course/cname[contains(text(),'XML')]/parent::Course
/School/count(Major/Teacher)
count(/School/Major/Class/Classpresident/pname[contains(text(),'陈')])+
count(/School/Major/Class/Student/pname[contains(text(),'陈')])
(//span[text()=‘匹配’])[2] '//div[contains(text(), " PM") or contains(text(), " AM")]'
XPathExpression expression = xPath.compile("//Student[@mcode+@classcode=48]");
XPathExpression expression = xPath.compile("//Student[contains(Name,'沈')]");
//Student[Name='沈1']/Tel/text()
/School/Major[@ID='M001']/@name
//Student[@mcode='23' and @classcode='56'] or
//Student[contains(@mcode, '23')]
//School/Major[not(contains(@ID, 'M001'))]/@name
data1 = selector.xpath("//input[@type='submit' and @name='fuck']");
data2 = selector.xpath("//input[@type='submit' or @name='fuck']");
data2 = selector.xpath("//input[@type='submit' and not(contains(@name,'fuck'))]");
data3 = selector.xpath("//input[starts-with(@id,'fuck')]"));
data4 = selector.xpath("//input[ends-with(@id,'fuck')]"));
data5 = selector.xpath("//input[contains(@id,'fuck')]"));
// 使用XPath表达式选择除了名为"dmCode"的节点以外的所有节点
String xpathExpression = "//*[not(name()='dmCode')]";
// 定义XPath表达式以查找systemDes节点下的所有名称包含dmCode的节点
String expression = "//systemDes//*[contains(name(), 'dmCode')]";
String expression = "//systemDes//*[name()='dmCode']";
//*[@id='count3' and contains(text(),'4')]
查询vm下的所有节点包含dm并且去除tr节点
//vm//*[dm and not(ancestor-or-self::tr)]
static void test4() throws Exception {
// build document
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// 提供对XML名称空间的支持。 默认false
factory.setNamespaceAware(true);
factory.setValidating(false);
DocumentBuilder documentBuilder = factory.newDocumentBuilder();
Document doc = documentBuilder.parse(FILE);
// build xpath
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xPath = xPathFactory.newXPath();
XPathExpression expression = xPath.compile("//Student/Name/text()");
Object evaluate = expression.evaluate(doc, XPathConstants.NODESET);
NodeList nodeList = (NodeList) evaluate;
for (int i = 0; i < nodeList.getLength(); i++) {
Node item = nodeList.item(i);
System.out.println(String.format("%s:%s", item.getNodeName(), item.getTextContent()));
NodeList childNodes = item.getChildNodes();
for (int j = 0; j < childNodes.getLength(); j++) {
Node node = childNodes.item(j);
if("#text".equals(node.getNodeName())) {
continue;
}
System.out.println(String.format("%s:%s", node.getNodeName(), node.getTextContent()));
}
System.out.println();
}
}
// 用Element方式
public static void element(NodeList list) {
for (int i = 0; i < list.getLength(); i++) {
Element element = (Element) list.item(i);
NodeList childNodes = element.getChildNodes();
for (int j = 0; j < childNodes.getLength(); j++) {
if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE) {
// 获取节点
System.out.print(childNodes.item(j).getNodeName() + ":");
// 获取节点值
System.out.println(childNodes.item(j).getFirstChild().getNodeValue());
}
}
}
}
// 用Node方式
public static void node(NodeList list) {
for (int i = 0; i < list.getLength(); i++) {
Node node = list.item(i);
NodeList childNodes = node.getChildNodes();
for (int j = 0; j < childNodes.getLength(); j++) {
if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE) {
System.out.print(childNodes.item(j).getNodeName() + ":");
System.out.println(childNodes.item(j).getFirstChild().getNodeValue());
}
}
}
}
<?xml version="1.0"?>
<root>
<text>This is some text.</text>
</root>
// 加载XML文档并获取Document对象
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(new File("C:\\abc\\a.xml"));
doc.getDocumentElement().normalize();
// 查找或创建元素
Element fontElement = doc.createElement("font");
fontElement.setAttribute("color", "red");
// 获取文本节点的值并创建一个新的文本节点
Node textNode = doc.getElementsByTagName("text").item(0).getFirstChild();
String textContent = textNode.getTextContent();
Text newTextNode = doc.createTextNode(textContent);
// 将新的文本节点添加到元素中
fontElement.appendChild(newTextNode);
// 将元素替换到原始位置
Node parentNode = textNode.getParentNode();
parentNode.replaceChild(fontElement, textNode);
// 保存更新后的XML文档
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("output.xml"));
transformer.transform(source, result);
System.out.println("XML document updated successfully.");
<?xml version="1.0" encoding="UTF-8" standalone="no"?><root>
<text><font color="red">This is some text.</font></text>
</root>
xml结合xsl
public static void Transform(String xmlFileName, String xslFileName,
String htmlFileName) throws Exception {
String xmlInput = "src/main/resources/input.xml";
// HTML 输出文件路径
String htmlOutput = "src/main/resources/output.html";
// 创建一个工厂对象用于创建Transformer实例
TransformerFactory transformerFactory = TransformerFactory.newInstance();
// 加载XSLT样式表(这个样式表定义了XML到HTML的转换规则)
StreamSource xsltStream = new StreamSource("src/main/resources/xslt_stylesheet.xsl");
Transformer transformer = transformerFactory.newTransformer(xsltStream);
// 创建XML输入流
Source xmlSource = new StreamSource(xmlInput);
// 创建HTML输出流
Result htmlResult = new StreamResult(new File(htmlOutput));
// 执行转换
transformer.transform(xmlSource, htmlResult);
System.out.println("XML has been transformed to HTML successfully.");
}