2014-03-07 228 views
0

我正在嘗試從java程序讀取XML文件。我能夠閱讀其內容。我發佈了我正在閱讀內容的XML文件。從java讀取XML文件

<?xml version="1.0" encoding="UTF-8" ?> 
<customer id="100"> 
<age>29</age> 
<name>lucifer</name> 
</customer> 

我能夠通過編寫Java程序的內容我張貼我的代碼..

import javax.xml.bind.annotation.XmlAttribute; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlRootElement; 

@XmlRootElement 
public class Customer { 

String name; 
int age; 
int id; 

public String getName() { 
    return name; 
} 

@XmlElement 
public void setName(String name) { 
    this.name = name; 
} 

public int getAge() { 
    return age; 
} 

@XmlElement 
public void setAge(int age) { 
    this.age = age; 
} 

public int getId() { 
    return id; 
} 

    @XmlAttribute 
public void setId(int id) { 
    this.id = id; 
    } 
    } 


public class CheckClass { 


public static void main(String[] args) { 

    try { 

     File file = new File("./file/NewFile.xml"); 
     JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class); 

     Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); 
     Customer customer = (Customer) jaxbUnmarshaller.unmarshal(file); 
     System.out.println(customer.age); 

     } catch (JAXBException e) { 
     e.printStackTrace(); 
     } 


    } 

} 

但我要讀,我可以not.This是我的XML這個XML文件值文件

<?xml version="1.0" encoding="UTF-8"?> 

<DBConfig ID="1" Name ="" DriverName="" HostName="localhost" PortName="" DBName=""  ServiceName="" User="" PassWord="" sid=""> 
     <TableConfig ID= "1" TableName=""> 
     </TableConfig> 
    </DBConfig> 

當我嘗試通過java類我得到這個錯誤該XML值..

com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions 
Class has two properties of the same name "DBName" 
this problem is related to the following location: 
    at public java.lang.String com.gamma.DBConf.getDBName() 
    at com.gamma.DBConf 
this problem is related to the following location: 
    at public java.lang.String com.gamma.DBConf.DBName 
    at com.gamma.DBConf 
    Class has two properties of the same name "sid" 
this problem is related to the following location: 
    at public java.lang.String com.gamma.DBConf.getSid() 
    at com.gamma.DBConf 
this problem is related to the following location: 
    at public java.lang.String com.gamma.DBConf.sid 
    at com.gamma.DBConf 

at  com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException$Builder.check(Unknown Source) 
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(Unknown Source) 
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(Unknown Source) 
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(Unknown Source) 
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(Unknown Source) 
at com.sun.xml.internal.bind.v2.ContextFactory.createContext(Unknown Source) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
at java.lang.reflect.Method.invoke(Unknown Source) 
at javax.xml.bind.ContextFinder.newInstance(Unknown Source) 
at javax.xml.bind.ContextFinder.newInstance(Unknown Source) 
at javax.xml.bind.ContextFinder.find(Unknown Source) 
at javax.xml.bind.JAXBContext.newInstance(Unknown Source) 
at javax.xml.bind.JAXBContext.newInstance(Unknown Source) 
at com.gamma.ReadXML.main(ReadXML.java:22) 

,這是我的java類

import javax.xml.bind.annotation.XmlAttribute; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlRootElement; 

@XmlRootElement 
public class DBConf { 

public String Name; 
public String DriverName; 
public String HostName; 
public String PortName; 
public String DBName; 
public String ServiceName; 
public String User; 
public String PassWord; 
public String sid; 


public String getName() { 
    return Name; 
} 
@XmlElement 
public void setName(String name) { 
    Name = name; 
} 
public String getDriverName() { 
    return DriverName; 
} 
@XmlElement 
public void setDriverName(String driverName) { 
    DriverName = driverName; 
} 
public String getHostName() { 
    return HostName; 
} 
@XmlElement 
public void setHostName(String hostName) { 
    HostName = hostName; 
} 
public String getPortName() { 
    return PortName; 
} 
@XmlElement 
public void setPortName(String portName) { 
    PortName = portName; 
} 
public String getDBName() { 
    return DBName; 
} 
@XmlElement 
public void setDBName(String dBName) { 
    DBName = dBName; 
} 
public String getServiceName() { 
    return ServiceName; 
} 
@XmlElement 
public void setServiceName(String serviceName) { 
    ServiceName = serviceName; 
} 
public String getUser() { 
    return User; 
} 
@XmlElement 
public void setUser(String user) { 
    User = user; 
} 
public String getPassWord() { 
    return PassWord; 
} 
@XmlElement 
public void setPassWord(String passWord) { 
    PassWord = passWord; 
} 
public String getSid() { 
    return sid; 
} 
@XmlElement 
public void setSid(String sid) { 
    this.sid = sid; 
} 


} 

這是主類

public class ReadXML { 

/** 
* @param args 
*/ 
public static void main(String[] args) { 
    try { 

     File file = new File("./file/dbconfig.xml"); 
     JAXBContext jaxbContext = JAXBContext.newInstance(DBConf.class); 

     Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); 
     DBConf db = (DBConf) jaxbUnmarshaller.unmarshal(file); 
     System.out.println(db.HostName); 

     } catch (JAXBException e) { 
     e.printStackTrace(); 
     } 

} 

} 

can anyone help 

回答

0

請注意,您在註釋屬性的元素。修復。

即使發生之後,如果問題 - @XmlAttribute(name = "HostName")註釋@XmlAccessorType(XmlAccessType.FIELD) 移至領域,而不是訪問方法 - 嘗試使用。

我不確定這是否是您的問題。我面臨類似的問題,這對我有幫助。我不保證它能解決你的問題,但表面上看來,上面看來可以解決它。

0

dbName,sid是屬性,但是你已經註釋了它們@XmlElement。將所有屬性更改爲@XmlAttribute。

+0

爲什麼它說類具有相同的名稱「數據庫名」 – lucifer

0

你爲什麼不使用 Xstream library

例子:

import com.thoughtworks.xstream.XStream; 
import com.thoughtworks.xstream.annotations.XStreamAlias; 
import com.thoughtworks.xstream.annotations.XStreamAsAttribute; 

@XStreamAlias("Cat") 
class Cat { 
    @XStreamAsAttribute 
    int age; 
    String name; 
} 

public class XStreamDemo { 

    public static void main(String[] args) { 

     XStream xstream = new XStream(); 
     xstream.processAnnotations(Cat.class); 

     String xml = "<Cat age='4' ><name>Garfield</name></Cat>"; 

     Cat cat = (Cat) xstream.fromXML(xml); 

     System.out.println("name -> " + cat.name); 
     System.out.println("age -> " + cat.age); 

    } 

} 

你需要在類路徑添加西河jar文件。

+0

我使用以前的職位越來越值properly.But什麼,我問的是XML結構我正在後會使用類的兩個屬性有可能從中獲得所有的值 – lucifer

0

使用這些類。

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "", propOrder = { 
    "value" 
}) 
@XmlRootElement(name = "TableConfig") 
public class TableConfig { 

@XmlValue 
protected String value; 
@XmlAttribute(name = "ID") 
protected Byte id; 
@XmlAttribute(name = "TableName") 
protected String tableName; 
public String getValue() { 
    return value; 
} 
public void setValue(String value) { 
    this.value = value; 
} 
public Byte getID() { 
    return id; 
} 

public void setID(Byte value) { 
    this.id = value; 
} 

public String getTableName() { 
    return tableName; 
} 

public void setTableName(String value) { 
    this.tableName = value; 
} 

} 


@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "", propOrder = { 
"tableConfig" 
}) 
@XmlRootElement(name = "DBConfig") 
public class DBConfig { 

@XmlElement(name = "TableConfig", required = true) 
protected TableConfig tableConfig; 
@XmlAttribute(name = "ID") 
protected Byte id; 
@XmlAttribute(name = "Name") 
protected String name; 
@XmlAttribute(name = "DriverName") 
protected String driverName; 
@XmlAttribute(name = "HostName") 
protected String hostName; 
@XmlAttribute(name = "PortName") 
protected String portName; 
@XmlAttribute(name = "DBName") 
protected String dbName; 
@XmlAttribute(name = "ServiceName") 
protected String serviceName; 
@XmlAttribute(name = "User") 
protected String user; 
@XmlAttribute(name = "PassWord") 
protected String passWord; 
@XmlAttribute 
protected String sid; 


public TableConfig getTableConfig() { 
    return tableConfig; 
} 


public void setTableConfig(TableConfig value) { 
    this.tableConfig = value; 
} 


public Byte getID() { 
    return id; 
} 

public void setID(Byte value) { 
    this.id = value; 
} 


public String getName() { 
    return name; 
} 


public void setName(String value) { 
    this.name = value; 
} 


public String getDriverName() { 
    return driverName; 
} 


public void setDriverName(String value) { 
    this.driverName = value; 
} 


public String getHostName() { 
    return hostName; 
} 

public void setHostName(String value) { 
    this.hostName = value; 
} 

public String getPortName() { 
    return portName; 
} 

public void setPortName(String value) { 
    this.portName = value; 
} 

public String getDBName() { 
    return dbName; 
} 

public void setDBName(String value) { 
    this.dbName = value; 
} 

public String getServiceName() { 
    return serviceName; 
} 

public void setServiceName(String value) { 
    this.serviceName = value; 
} 
public String getUser() { 
    return user; 
} 
public void setUser(String value) { 
    this.user = value; 
} 
public String getPassWord() { 
    return passWord; 
} 

public void setPassWord(String value) { 
    this.passWord = value; 
} 
public String getSid() { 
    return sid; 
} 
public void setSid(String value) { 
    this.sid = value; 
} 

}