W3C进阶Xml篇
2023-12-31 14:21:47
<?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");
}
});
// 创建一个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去除<?xml version="1.0" encoding="UTF-8"?>
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();
// 查找或创建<font>元素
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);
// 将新的文本节点添加到<font>元素中
fontElement.appendChild(newTextNode);
// 将<font>元素替换到原始位置
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>
文章来源:https://blog.csdn.net/qq_37792401/article/details/135228642
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!