简体   繁体   English

从Java读取XML文件

[英]Reading XML files from java

I am trying to read an XML file from java program. 我正在尝试从Java程序读取XML文件。 I am able to read its contents.I am posting the XML file from which i am reading contents. 我能够读取其内容。我正在发布要从中读取内容的XML文件。

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

i am able to write its contents through java program i am posting my code.. 我能够通过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();
      }


   }

 }

But i have to read values from this XML file which i can not.This is my XML file 但是我必须从我不能读取的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>

When i am trying to access this xml values through java class i am getting this error.. 当我尝试通过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)

and this is my java classes 这是我的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;
}


}

And this is the main class 这是主班

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

Note that you are annotating Attribute as Element. 请注意,您正在将属性注释为元素。 Fix that. 解决这个。

Even after that if problem occurs - Try using - @XmlAccessorType(XmlAccessType.FIELD) Move @XmlAttribute(name = "HostName") annotations to fields instead of accessor methods. 即使出现问题,也可以在此之后-尝试使用- @XmlAccessorType(XmlAccessType.FIELD)@XmlAttribute(name = "HostName")注释移至字段而不是访问器方法。

I am not sure if this is your problem. 我不确定这是否是您的问题。 I faced a similar problem and this helped me. 我遇到了类似的问题,这对我有所帮助。 I wont guarantee that it will solve your problem but prima facie, it appears that above can fix it. 我不能保证它将解决您的问题,但是表面看来,上面的方法可以解决该问题。

dbName, sid are Attributes, but you have annotated them @XmlElement. dbName,sid是属性,但是您已在@XmlElement处对其进行了注释。 change all the attributes to @XmlAttribute. 将所有属性更改为@XmlAttribute。

Why don't you use Xstream library 为什么不使用Xstream库

Example: 例:

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);

    }

}

You need to add Xstream jar files in to classpath. 您需要将Xstream jar文件添加到classpath中。

Use these classes. 使用这些类。

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM