从java读取XML文件

时间:2014-03-07 09:44:16

标签: java xml

我正在尝试从java程序中读取XML文件。我能够阅读其内容。我正在发布我正在阅读内容的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();
      }


   }

 }

但是我必须从这个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

4 个答案:

答案 0 :(得分:0)

请注意,您将属性注释为元素。修复它。

即便如此,如果出现问题 - 尝试使用 - @XmlAccessorType(XmlAccessType.FIELD)@XmlAttribute(name = "HostName")注释移动到字段而不是存取方法。

我不确定这是不是你的问题。我遇到了类似的问题,这对我有所帮助。我不保证它会解决你的问题,但表面上看,似乎上面可以解决它。

答案 1 :(得分:0)

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

答案 2 :(得分: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);

    }

}

您需要将Xstream jar文件添加到类路径中。

答案 3 :(得分: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;
}

}