使用JDOM在java中进行XML解析(getChildren方法不返回List)

时间:2012-07-01 07:57:09

标签: java xml jdom

对于其根有25个子节点的xml文件,getChildren方法返回list.size = 0 !!

这是我的代码:

public static void main(String[] args) {

  SAXBuilder builder = new SAXBuilder();
       File xmlFile = new File("g:\\*");
        try {

    Document document = (Document) builder.build(xmlFile);
    Element rootNode = document.getRootElement();
    List list = rootNode.getChildren("job",Namespace.getNamespace("Montage"));

    for (int i = 0; i < list.size(); i++) 
                {

       Element node = (Element) list.get(i);
               System.out.println("ID : " + node.getAttributeValue("id"));
       System.out.println("Run Time : " + node.getAttributeValue("runtime"));
            }
        }
             catch (IOException io) {
    System.out.println(io.getMessage());
  } catch (JDOMException jdomex) {
    System.out.println(jdomex.getMessage());
  }

    }

这里是xml文件的一部分:

<adag xmlns="http://pegasus.isi.edu/schema/DAX"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pegasus.isi.edu/schema/DAX http://pegasus.isi.edu/schema/dax-2.1.xsd" version="2.1" count="1" index="0" name="test" jobCount="25" fileCount="0" childCount="20">
 <job id="ID00000" namespace="Montage" name="mProjectPP" version="1.0" runtime="13.39">
  <uses file="region.hdr" link="input" register="true" transfer="true" optional="false" type="data" size="304" /> 
  <uses file="2mass-atlas-ID00000s-jID00000.fits" link="input" register="true" transfer="true" optional="false" type="data" size="4222080" /> 
  <uses file="p2mass-atlas-ID00000s-jID00000.fits" link="output" register="true" transfer="true" optional="false" type="data" size="4167312" /> 
  <uses file="p2mass-atlas-ID00000s-jID00000_area.fits" link="output" register="true" transfer="true" optional="false" type="data" size="4167312" /> 
  </job>

有什么问题?

1 个答案:

答案 0 :(得分:0)

您的job元素有一个名为namespace的属性,其值为“蒙太奇”,但这不会使作业元素成为“蒙太奇”命名空间的一部分。

只需使用

List list = rootNode.getChildren("job");