java递归获取xml节点数据

<?xml version="1.0" encoding="UTF-8"?>

<buildCloud name="buildCloud"> <job id="1">test</job> <result id="">success</result> <downTasks name="downTasks" startTime='10' endTime='20'>3 <downTask name="ICPCI" startTime='10' endTime='20'>3</downTask> <downTask name="BuildCloud" startTime='20' endTime='30'>3</downTask> </downTasks> <agentTask name="CCT" startTime='1' endTime='2'> 3 <task name="cct_common" startTime='11' endTime='21'>3</task> <task name="cct_anget" startTime='111' endTime='121'> 3 </task> </agentTask>

	<compiste name="simint_commonLP" startTime='A' endTime='B'>
		3
		<current name="simintCurr" startTime='VV' endTime='LL'>
			3
			<agentTask name="CCTS" startTime='mk' endTime='LM'>
				3
				<task name="cct_commonS" startTime='1' endTime='ds'>3</task>
				<task name="cct_angetS" startTime='1' endTime='sd2'>3</task>
			</agentTask>
		</current>
	</compiste>
  


<current name="simint_common" startTime='A' endTime='V'>
	3
	<compiste name="simint_commonLP" startTime='A' endTime='B'>
		3
		<current name="simintCurr" startTime='VV' endTime='LL'>
			3
			<agentTask name="CCTS" startTime='mk' endTime='LM'>
				3
				<task name="cct_commonS" startTime='1' endTime='ds'>3</task>
				<task name="cct_angetS" startTime='1' endTime='sd2'>3</task>
			</agentTask>
		</current>
	</compiste>
</current>

</buildCloud>

package task;

import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List;

import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.JDOMException; import org.jdom2.input.SAXBuilder;

public class Parse2 { public static Element getRootElement(String file) { try { Document doc = new SAXBuilder().build(new File(file)); return doc.getRootElement(); } catch (JDOMException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }

public static List<String> getElementStringList(String nodeName,
		Element root) {
	List<Element> childList = root.getChildren();
	List<String> list = new ArrayList<String>();
	for (Element e : childList) {
		if (e.getAttribute("name") != null) {
			String content = e.getName() + ","
					+ e.getAttributeValue("name") + ","
					+ e.getAttributeValue("startTime") + ","
					+ e.getAttributeValue("endTime") + ","
					+ e.getText().trim();
			list.add(content);
			list.addAll(getElementStringList(e.getName(), e));
		}
	}

	return list;
}

public static String getElementList(String nodeName, Element root) {
	List<Element> childList = root.getChildren();

	StringBuffer buffer = new StringBuffer();
	for (Element e : childList) {
		if (e.getAttribute("name") != null) {
			String content = e.getName() + ","
					+ e.getAttributeValue("name") + ","
					+ e.getAttributeValue("startTime") + ","
					+ e.getAttributeValue("endTime") + ","
					+ e.getText().trim();
			buffer.append(content).append("\n");
			// System.out.println(content);
			buffer.append(getElementList(e.getName(), e));
		}
	}

	return buffer.toString();
}

public static void main(String args[]) {
	Element root = getRootElement("D:\\parse.xml");
	// String s = getElementList("buildCloud", root);
	// System.err.println(s);

	List<String> list = getElementStringList("buildCloud", root);
	for (String str : list) {
		System.out.println(str);
	}
}

}

转载于:https://my.oschina.net/u/946001/blog/316764

使用Java获取XML数据中的所有XPath路径可以使用递归方法来实现,遍历XML节点并存储XPath路径。以下是一个示例代码: ```java import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathFactory; import java.util.ArrayList; import java.util.List; public class XpathExtractor { public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse("path/to/xml/file.xml"); List<String> xpaths = getXPaths(doc); for (String xpath : xpaths) { System.out.println(xpath); } } public static List<String> getXPaths(Node node) throws Exception { List<String> xpaths = new ArrayList<>(); XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); // 获取节点的XPath XPathExpression xpathExp = xpath.compile(getNodeXPath(node)); String xpathValue = (String) xpathExp.evaluate(node, XPathConstants.STRING); xpaths.add(xpathValue); // 遍历子节点 NodeList children = node.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if (child.getNodeType() == Node.ELEMENT_NODE) { xpaths.addAll(getXPaths(child)); } } return xpaths; } private static String getNodeXPath(Node node) { StringBuilder builder = new StringBuilder(); for (; node != null; node = node.getParentNode()) { int index = getNodeIndex(node); String nodeName = node.getNodeName(); builder.insert(0, "/" + nodeName + "[" + index + "]"); } return builder.toString(); } private static int getNodeIndex(Node node) { int index = 1; for (Node sibling = node.getPreviousSibling(); sibling != null; sibling = sibling.getPreviousSibling()) { if (sibling.getNodeType() == node.getNodeType() && sibling.getNodeName().equals(node.getNodeName())) { index++; } } return index; } } ``` 该示例代码使用了递归方法来获取XML数据中的所有XPath路径。getXPaths()方法接收一个XML节点,将该节点的XPath路径添加到一个列表中,并递归遍历节点的子节点,直到所有节点都被遍历完为止。该示例代码中还包括了getNodeXPath()和getNodeIndex()方法,用于获取节点的XPath路径和节点的索引。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值