w3c进阶Xml篇

java对象相互转换xml jakarta.xml.bind-api

	// 将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("<!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')]";  
	String xPathExpression = ".//*[local-name()='" + nodeToCheck.getNodeName() + "' and normalize-space(text())='" + nodeToCheck.getTextContent().trim() + "']";
	// 定义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>

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.");
}
  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

有知识的山巅

文章对你有用,学到了知识。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值