Question

i have an XML document that have some configuration information i have to access the information from a java class.I am able to access the information using java unmarshalling,but the problem is that i can not access a specific portion of the xml file.Every time i am accessing the information i have to take the root tag first i.e i only can access the root tag elements,but i need to access the tag that is in the another section.This is my XML file

<?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>

i can access only the elements of the adapter tag.but what i need to use dbconf.. here i am posting the class es i am using to get the value of the adapter tag.The model class

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

}

The main class to get the values

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

    }

}
Was it helpful?

Solution

You can use a StAX XMLStreamReader to advance parse the XML document. Then advance to the XML element you want to unmarshal. Then use the unmarshal method that takes XMLStreamReader as a parameter.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top