使用JDOM API读取xml文件

时间:2014-06-04 10:22:03

标签: java xml jdom

<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和他们的孩子对所有人的请求回复。

请帮助我

3 个答案:

答案 0 :(得分:1)

您只需要使用列表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());
    }

希望它有所帮助。

答案 1 :(得分:0)

http://www.javacodegeeks.com/2013/05/parsing-xml-using-dom-sax-and-stax-parser-in-java.html


http://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/

<employees>
  <employee id="111">
    <firstName>Rakesh</firstName>
    <lastName>Mishra</lastName>
    <location>Bangalore</location>
  </employee>
  <employee id="112">
    <firstName>John</firstName>
    <lastName>Davis</lastName>
    <location>Chennai</location>
  </employee>
  <employee id="113">
    <firstName>Rajesh</firstName>
    <lastName>Sharma</lastName>
    <location>Pune</location>
  </employee>
</employees>


public class DOMParserDemo {

  public static void main(String[] args) throws Exception {
//Get the DOM Builder Factory
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

//Get the DOM Builder
DocumentBuilder builder = factory.newDocumentBuilder();

//Load and Parse the XML document
//document contains the complete XML as a Tree.
Document document = builder.parse(
    ClassLoader.getSystemResourceAsStream("xml/employee.xml"));

//Iterating through the nodes and extracting the data.
NodeList nodeList = document.getDocumentElement().getChildNodes();

for (int i = 0; i < nodeList.getLength(); i++) {

  //We have encountered an <employee> tag.
  Node node = nodeList.item(i);
  if (node instanceof Element) {
   // Attribute value
   System.out.println(node.getAttributes().getNamedItem("id").getNodeValue());

    NodeList childNodes = node.getChildNodes();
    for (int j = 0; j < childNodes.getLength(); j++) {
      Node cNode = childNodes.item(j);

      //Identifying the child tag of employee encountered. 
      if (cNode instanceof Element) {
        String content = cNode.getLastChild().getTextContent().trim();
        System.out.println(cNode.getNodeName());
        System.out.println(content);
      }
    }       
  }
}

答案 2 :(得分:0)

我解决了。这是代码

public static void main (String argv []) throws Exception{

      SAXBuilder builder = new SAXBuilder();
      File xmlFile = new File("sample.xml");
      Document document = (Document) builder.build(xmlFile);
      List<Element> songElements = document.getRootElement().getChildren("Flow");         
      for(Element element : songElements) {

          String flowName =element.getAttribute("name").getValue();
          System.out.println(flowName);
          List<Element> flowAPiList = element.getChildren(); 

          for(int i = 0 ; i<flowAPiList.size() ; i++){
              Element singleAPIName = flowAPiList.get(i);
              System.out.println(singleAPIName.getAttribute("name").getValue());
              System.out.println(singleAPIName.getAttribute("value").getValue());
              //System.out.println(singleAPIName.getChild("Request").getValue());
              //System.out.println(singleAPIName.getChild("Response").getValue());

          }     

          System.out.println();

      } 


}//end of main