在java中用dom4j解析xml

直接上代码


package test01;

import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class TestXmlParse
{

public static void main(String[] args)
{
String xmlStr = ""
+ "xml解析"
+ "

"
+ "
这是div
";
Document document;
try
{
document = DocumentHelper.parseText(xmlStr);
Element root = document.getRootElement();
List divs = root.element("body").elements("div");
Element div3 = null;
for (Element div : divs)
{
if ("div3".equals(div.attribute("id").getValue()))
{
div3 = div;
break;
}
}
System.out.println(div3.attribute("class").getValue());
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
}


jar包:dom4j-1.6.1.jar

你可能感兴趣的:(java琐碎知识)