<?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("<!DOCTYPE")) {
doctypeBuilder.append(item).append("\n");
}
});
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(new File(""));
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
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();
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(),'陈')])
(
XPathExpression expression = xPath.compile("//Student[@mcode+@classcode=48]");
XPathExpression expression = xPath.compile("//Student[contains(Name,'沈')]");
/School/Major[@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')]"));
String xpathExpression = "//*[not(name()='dmCode')]";
String expression = "//systemDes//*[contains(name(), 'dmCode')]";
String expression = "//systemDes//*[name()='dmCode']";
查询vm下的所有节点包含dm并且去除tr节点
static void test4() throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(false);
DocumentBuilder documentBuilder = factory.newDocumentBuilder();
Document doc = documentBuilder.parse(FILE);
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();
}
}
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());
}
}
}
}
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>
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);
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>