使用Unmarshal访问XML文档的特定部分

时间:2014-03-07 17:57:28

标签: java xml jaxb unmarshalling

我有一个XML文档,它有一些配置信息,我必须从java类访问信息。我能够使用java unmarshalling访问信息,但问题是我无法访问xml的特定部分file。每次我访问信息时我都必须首先获取根标签,即我只能访问根标签元素,但我需要访问另一部分中的标签。这是我的XML文件

<?xml version="1.0" encoding="UTF-8"?>
<adapter  name="Hua_GPRS_CS5_4.7"  type="ASN.1"  version="1.0"  description="Huawie GPRS Output CDR Version 4.7"
      execclass="hua.gprs.HuaGPRSFileExecutor" parallel="1" mode="server" statusfile="HuaGPRSStatus.csv" merge_factor="1" active="true">

<dirconfig>
    <Poll protocol="" host="" path="./data/huagprs/source" pwd="" user="" clogging="true" startdir="" enddir=""
     pattern="(.*)\.(\d{6})(\d{6})(\d{4})_(.*)" metafields="fileName,exchange,fileDate,fileTime,fileSeq,junk"/>
    <Source path="./data/huagprs/source" clogging="false"/>
    <Backup path="./data/huagprs/backup" active="false"/>
    <Staging path="./data/huagprs/staging" clogging="false"/>
    <Output path="./data/huagprs/output" clogging="false" compress="gz"/>
    <Error  path="./data/huagprs/error" clogging="true"/>
    <Target protocol="" host="" path="/" pwd="" user="" active="true"/>
</dirconfig>
<dbConf id="1" name ="jjj" drivername="oracle.jdbc.driver.OracleDriver" hostname="localhost" portname="1521" dbname="rrr" servicename="orcl" user="param" password="param" sid="orcl">
    <TableConfig ID= "1" TableName="">
    </TableConfig>
 </dbConf>
</adapter>

我只能访问适配器标签的元素。但是我需要使用dbconf ..这里我发布的类es我用来获取适配器标签的值。模型类

import javax.xml.bind.annotation.XmlAttribute;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Adapter {

String name;
String type;
String version;
String description;
String execclass;
String parallel;
String mode;
String statusfile;
String merge_factor;
String active;
String drivername;

public String getDrivername() {
    return drivername;
}
@XmlAttribute
public void setDrivername(String drivername) {
    this.drivername = drivername;
}
public String getName() {
    return name;
}
@XmlAttribute
public void setName(String name) {
    this.name = name;
}
public String getType() {
    return type;
}
@XmlAttribute
public void setType(String type) {
    this.type = type;
}
public String getVersion() {
    return version;
}
@XmlAttribute
public void setVersion(String version) {
    this.version = version;
}
public String getDescription() {
    return description;
}
@XmlAttribute
public void setDescription(String description) {
    this.description = description;
}
public String getExecclass() {
    return execclass;
}
@XmlAttribute
public void setExecclass(String execclass) {
    this.execclass = execclass;
}
public String getParallel() {
    return parallel;
}
@XmlAttribute
public void setParallel(String parallel) {
    this.parallel = parallel;
}
public String getMode() {
    return mode;
}
@XmlAttribute
public void setMode(String mode) {
    this.mode = mode;
}
public String getStatusfile() {
    return statusfile;
}
@XmlAttribute
public void setStatusfile(String statusfile) {
    this.statusfile = statusfile;
}
public String getMerge_factor() {
    return merge_factor;
}
@XmlAttribute
public void setMerge_factor(String merge_factor) {
    this.merge_factor = merge_factor;
}
public String getActive() {
    return active;
}
@XmlAttribute
public void setActive(String active) {
    this.active = active;
    }

}

获取值的主要类

import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;


public class ReadXML {

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

        File file = new File("./file/config.ds");

        JAXBContext jaxbContext2 = JAXBContext.newInstance(Adapter.class);


        Unmarshaller jaxbUnmarshaller2 = jaxbContext2.createUnmarshaller();

        Adapter db2 = (Adapter) jaxbUnmarshaller2.unmarshal(file);

        System.out.println(db2.active);





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

    }

}

1 个答案:

答案 0 :(得分:0)

您可以使用StAX XMLStreamReader来提升解析XML文档。然后前进到要解组的XML元素。然后使用以XMLStreamReader作为参数的unmarshal方法。

相关问题