Java 解析Xml(基本满足你所有的取值情况)

package cn.com.ebidding.web.tool;

import java.util.List;

import org.dom4j.*;

public class DocumentParse {

	public static void main(String[] args) {
		String xml = 
				"" +
					"C4201000016" +
					"A01" +
					"" +
						"CACG2018000001" +
						"XXX-2012-0101" +
						"XXX招标项目" +
						"招标内容与范围" +
						"招标执行组织代码" +
						"张三" +
						"010-02224222" +
						"1" +
						"2" +
						"201801021221" +
						"C4201000016" +
					"" +
				"";
		try {
			Document xmldoc = DocumentHelper.parseText(xml);
			Element rootEl = xmldoc.getRootElement();
			// 得到所有跟节点
			List rootlist = rootEl.elements();
			String msgFromID = rootEl.elementText("msgFromID");
			System.out.println("根节点:" + msgFromID);
			String planCode = rootEl.element("msgBody").element("msgNode").elementText("planCode");
			System.out.println("根节点的子节点:" + planCode);
			// 得到有子节点的根节点
			List bodyList = rootEl.selectNodes("msgBody");
			for(int i=0; i nodelist = elementNode.elements();
				for (Element ele : nodelist) {
					// 如果节点内容包含html
					String context = ele.asXML();
					if(context.contains("

") || context.contains("") || context.contains("") || context.contains("")){ context.replace("<"+ele.getName()+">", "").replace("", ""); } System.out.println(ele.getName() + ":" + ele.getStringValue()); } } } catch (Exception e) { e.printStackTrace(); } } }

你可能感兴趣的:(JAVA,java,xml,根节点,子节点,节点值为html)