<ApiModeling>
<Flow name="PreFlow">
<ApiName name="/user/employeeExist/1" value="25182ab0-dd35-4e1b-ac1c-a56bece15119;1.0">
<Request />
<Response />
</ApiName>
<ApiName name="api1" value="api1Value">
<Request />
<Response />
</ApiName>
</Flow>
<Flow name="MainFlow">
<ApiName name="user/employee/1" value="12dfwcjgs-sjvndjsf">
<Request />
<Response />
</ApiName>
<ApiName name="api1" value="api1Value">
<Request />
<Response />
</ApiName>
</Flow>
<Flow name="PostFlow">
<ApiName name="api1" value="api1Value">
<Request />
<Response />
</ApiName>
<ApiName name="api2" value="api2Value">
<Request />
<Response />
</ApiName>
<ApiName name="api3" value="api3Value">
<Request />
<Response />
</ApiName>
<ApiName name="api4" value="api4Value">
<Request />
<Response />
</ApiName>
<ApiName name="api5" value="api5Value">
<Request />
<Response />
</ApiName>
<ApiName name="api6" value="api6Value">
<Request />
<Response />
</ApiName>
<ApiName name="api7" value="api7Value">
<Request />
<Response />
</ApiName>
</Flow>
</ApiModeling>
我能够获得总流量为 3。但是我无法获得元素 ApiName 及其子元素的所有请求响应。
这就是我正在尝试的方法。
SAXBuilder builder = new SAXBuilder();
File xmlFile = new File("flowmodeling.xml");
Document document = (Document) builder.build(xmlFile);
Element e = document.getRootElement();
List songElements = document.getRootElement().getChildren("Flow");
我想获得 ApiName 及其子项的所有请求响应。
请帮帮我
最佳答案
您只需迭代列表 SongElements 即可检索所有元素,例如:
SAXBuilder builder = new SAXBuilder();
File xmlFile = new File("flowmodeling.xml");
Document document = (Document) builder.build(xmlFile);
Element e = document.getRootElement();
List<Element> songElements = document.getRootElement().getChildren("Flow");
for(Element element : songElements) {
Element apiName = element.getChild("ApiName");
System.out.println(apiName.getAttribute("name"));
Element request = apiName.getChild("Request");
System.out.println(request.getValue());
Element response = apiName.getChild("Response");
System.out.println(response.getValue());
}
希望有帮助。
关于java - 使用 JDOM API 读取 xml 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24034894/