XML子节点属性

时间:2011-04-28 18:58:27

标签: java

我正在尝试检索父节点和子节点的属性。我试着用这个例子 showed here但是它给了我“不能应用于给定类型”。

static class Entry {
   final String ED,Home, Shift, Detail;
   Entry(String ED,String Home, String Shift,String Detail) {
      this.ED= ED;
      this.Home = Home;
      this.Shift = Shift;
      this.Detail = Detail;
      Node entryNode = entriesNodeList.item(i);
      entries.add(new Entry(
         evalString(entryNode, "Home/@name"),  
         evalString(entryNode, "Home/@starDate"),
         evalString(entryNode, "Home/@endDate"), 
         evalString(entryNode, "Home/Shift/@employeeID"),
         evalString(entryNode, "Home/Shift/@start"),
         evalString(entryNode, "Home/Shift/@end"), 
         evalString(entryNode, "Home/Shift/Detail/@start"), 
         evalString(entryNode, "Home/Shift/Detail/@end")

XML文件:

`<ED CS="BLUE" Timestamp="2011-03-23 13:57:01" xmlns:js="TEST/js">
 <Home name="57511" startDate="2011-03-12" endDate="2011-03-18">
 <Shift ID="1170108" start="2011-03-12 0718" end="2011-03-12 1636" >
 <Detail type="Meal" start="2011-03-12 1218" end="2011-03-12 1248" /> 
 </Shift>
 </ED>`

1 个答案:

答案 0 :(得分:0)

“Entry”类的构造函数只接受4个String参数,并且您尝试使用它来调用它。该类似乎只有4个字段。也许您想要为ED实际引入复杂的类(包括字段CS,Tim​​estamp,Home和Shift),Home,Shift和Detail,它们将您的xml文件表示为Java对象层次结构。