sax简单解析xml的示例代码分享
package com.zkn.xmlparse.text;
import java.io.File;
import java.util.Iterator;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
* <p>Title: </p>
*
* <p>Description: </p>
<div class="aritcle_card">
<a class="aritcle_card_img" href="/ai/1125">
<img src="https://img.php.cn/upload/ai_manual/000/000/000/175680079579055.png" alt="OneStory">
</a>
<div class="aritcle_card_info">
<a href="/ai/1125">OneStory</a>
<p>OneStory 是一款创新的AI故事生成助手,用AI快速生成连续性、一致性的角色和故事。</p>
<div class="">
<img src="/static/images/card_xiazai.png" alt="OneStory">
<span>319</span>
</div>
</div>
<a href="/ai/1125" class="aritcle_card_btn">
<span>查看详情</span>
<img src="/static/images/cardxiayige-3.png" alt="OneStory">
</a>
</div>
*
* @since:2015-6-3 下午12:40:10
*
*/
public class XMLParseTest02 {
public static void main(String[] args) throws Exception {
SAXReader reader = new SAXReader();
Document document = reader.read(new File("src/mapping.xml"));
Element element = document.getRootElement();
getElement(element);
}
public static void getElement(Element element) {
/**
* 得到tag名字
*/
String elements = element.getName();
Iterator<?> ite = element.attributes().iterator();
String attrName = "";
while(ite.hasNext()) {
Attribute attr = (Attribute)ite.next();
attrName += attr.getName() + "="+attr.getValue()+" ";
}
System.out.println("元素名字:"+elements+" 属性:"+attrName);
Iterator<?> it = element.elements().iterator();
while(it.hasNext()){
Element ele = (Element)it.next();
//递归调用
getElement(ele);
}
}
}以上就是SAX简单解析XML的示例代码分享的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号