从java读取XML文件

问题描述:

我正在尝试从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 

请注意,您在注释属性的元素。修复。

即使发生之后,如果问题 - @XmlAttribute(name = "HostName")注释@XmlAccessorType(XmlAccessType.FIELD) 移至领域,而不是访问方法 - 尝试使用。

我不确定这是否是您的问题。我面临类似的问题,这对我有帮助。我不保证它能解决你的问题,但表面上看来,上面看来可以解决它。

dbName,sid是属性,但是你已经注释了它们@XmlElement。将所有属性更改为@XmlAttribute。

+0

为什么它说类具有相同的名称“数据库名” – lucifer

你为什么不使用 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

使用这些类。

@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; 
} 

}