xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<filelists xmlns="http://linux.duke.edu/metadata/filelists" packages="5890">
<package pkgid="e504c08570baa3de15002e9593d8f5460d58451232cdf2a8af989ca849bc1a6e" name="CUnit" arch="loongarch64">
<version epoch="0" ver="2.1.3" rel="17.lns8"/>
<file type="dir">/usr/lib/.build-id</file>
<file type="dir">/usr/lib/.build-id/18</file>
<file>/usr/lib/.build-id/18/e3d9aa7a1abf3fd08a6a062ac8a9400484c1ce</file>
<file>/usr/lib64/libcunit.so.1</file>
<file>/usr/lib64/libcunit.so.1.0.1</file>
<file type="dir">/usr/share/CUnit</file>
<file>/usr/share/CUnit/CUnit-List.dtd</file>
<file>/usr/share/CUnit/CUnit-List.xsl</file>
<file>/usr/share/CUnit/CUnit-Run.dtd</file>
<file>/usr/share/CUnit/CUnit-Run.xsl</file>
<file>/usr/share/CUnit/Memory-Dump.dtd</file>
<file>/usr/share/CUnit/Memory-Dump.xsl</file>
<file type="dir">/usr/share/doc/CUnit</file>
<file>/usr/share/doc/CUnit/AUTHORS</file>
<file>/usr/share/doc/CUnit/COPYING</file>
<file>/usr/share/doc/CUnit/ChangeLog</file>
<file>/usr/share/doc/CUnit/NEWS</file>
<file>/usr/share/doc/CUnit/README</file>
<file>/usr/share/doc/CUnit/TODO</file>
<file>/usr/share/doc/CUnit/VERSION</file>
</package>
<package pkgid="54184c0518d341116a6565787758e1f148d607d3431ab27bd28c4f16a38074f4" name="CUnit-devel" arch="loongarch64">
<version epoch="0" ver="2.1.3" rel="17.lns8"/>
<file type="dir">/usr/include/CUnit</file>
<file>/usr/include/CUnit/Automated.h</file>
<file>/usr/include/CUnit/Basic.h</file>
<file>/usr/include/CUnit/CUError.h</file>
<file>/usr/include/CUnit/CUnit.h</file>
<file>/usr/include/CUnit/CUnit_intl.h</file>
<file>/usr/include/CUnit/Console.h</file>
<file>/usr/include/CUnit/MyMem.h</file>
<file>/usr/include/CUnit/TestDB.h</file>
<file>/usr/include/CUnit/TestRun.h</file>
<file>/usr/include/CUnit/Util.h</file>
<file>/usr/lib64/libcunit.so</file>
<file>/usr/lib64/pkgconfig/cunit.pc</file>
<file type="dir">/usr/share/doc/CUnit/html</file>
<file>/usr/share/doc/CUnit/html/CUnit_doc.css</file>
<file>/usr/share/doc/CUnit/html/error_handling.html</file>
<file>/usr/share/doc/CUnit/html/fdl.html</file>
<file type="dir">/usr/share/doc/CUnit/html/headers</file>
<file>/usr/share/doc/CUnit/html/headers/Automated.h</file>
<file>/usr/share/doc/CUnit/html/headers/Basic.h</file>
<file>/usr/share/doc/CUnit/html/headers/CUCurses.h</file>
<file>/usr/share/doc/CUnit/html/headers/CUError.h</file>
<file>/usr/share/doc/CUnit/html/headers/CUnit.h</file>
<file>/usr/share/doc/CUnit/html/headers/CUnit_intl.h</file>
<file>/usr/share/doc/CUnit/html/headers/Console.h</file>
<file>/usr/share/doc/CUnit/html/headers/MyMem.h</file>
<file>/usr/share/doc/CUnit/html/headers/TestDB.h</file>
<file>/usr/share/doc/CUnit/html/headers/TestRun.h</file>
<file>/usr/share/doc/CUnit/html/headers/Util.h</file>
<file>/usr/share/doc/CUnit/html/headers/Win.h</file>
<file>/usr/share/doc/CUnit/html/index.html</file>
<file>/usr/share/doc/CUnit/html/introduction.html</file>
<file>/usr/share/doc/CUnit/html/managing_tests.html</file>
<file>/usr/share/doc/CUnit/html/running_tests.html</file>
<file>/usr/share/doc/CUnit/html/test_registry.html</file>
<file>/usr/share/doc/CUnit/html/writing_tests.html</file>
<file>/usr/share/man/man3/CUnit.3.gz</file>
</package>
</filelists>
解析Demo:
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.File;
import java.util.List;
public class SAXReaderXML {
public static void main(String[] args) throws Exception {
SAXReader reader = new SAXReader();
File xmlfile = new File("src/main/resources/filelists.xml");
Document document = reader.read(xmlfile);//从xml文件获取数据
Element root = document.getRootElement();//获取根元素
System.out.println(root.getName());
List<Element> list = root.elements(); //得到所有元素
for (Element element : list) { //循环输出全部element的相关属性
String name = element.attributeValue("name");
String arch = element.attributeValue("arch");
List<Element> list2 = element.elements(); //得到emp元素下的子元素
int i=0;
for (Element e : list2) {
if(e.getName().equals("version")){
String ver = e.attributeValue("ver");
String rel = e.attributeValue("rel");
System.out.println(name+"-"+ver+"-"+rel+"."+arch+".rpm"); //获取子元素属性
}
}
}
}
}
输出结果:
filelists
CUnit-2.1.3-17.lns8.loongarch64.rpm
CUnit-devel-2.1.3-17.lns8.loongarch64.rpm
GConf2-3.2.6-22.lns8.loongarch64.rpm
GConf2-devel-3.2.6-22.lns8.loongarch64.rpm
HdrHistogram_c-0.9.13-2.lns8.loongarch64.rpm
HdrHistogram_c-devel-0.9.13-2.lns8.loongarch64.rpm
Judy-1.0.5-18.module_lns8+217+4d875839.loongarch64.rpm