Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6664339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:35:58+00:00 2026-05-26T02:35:58+00:00

I have created an XML file and DTD which can be found at the

  • 0

I have created an XML file and DTD which can be found at the HERE.

I have written a code, but it works till one level, then it doesnot works properly. I have also created certain objects to store the value of the xml file. But i am only able to traverse till sheet tag of the xml, then it doesnot works properly.

Recon recon = new Recon();

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(configFile);
doc.getDocumentElement().normalize();

System.out.println("Root Element : " + doc.getDocumentElement().getNodeName());
String outputPath = doc.getDocumentElement().getAttribute("outputPath");
String withCompareFilePath = doc.getDocumentElement().getAttribute("withCompareFile");
String toCompareFilePath = doc.getDocumentElement().getAttribute("toCompareFile");

recon.setOutputPath(outputPath);
recon.setToCompareFile(new File(toCompareFilePath));
recon.setWithCompareFile(new File(withCompareFilePath));

NodeList sheetNodeList = doc.getElementsByTagName("sheet");

List<ReconSheet> reconSheets = new ArrayList<ReconSheet>();

for(int i = 0; i< sheetNodeList.getLength() ; i++) {
  Node tempNode = sheetNodeList.item(i);
  ReconSheet reconSheet = new ReconSheet();
  NamedNodeMap attMap = tempNode.getAttributes();
  Node sheetNode = attMap.getNamedItem("sheetNumber");
  String sheetNumber = sheetNode.getNodeValue();
  reconSheet.setSheetNumber(Integer.parseInt(sheetNumber));
  NodeList list = tempNode.getChildNodes();
  for(int j = 0; j< list.getLength(); j++) {
    Node inNode = list.item(j);
    System.out.println(inNode);
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:35:59+00:00Added an answer on May 26, 2026 at 2:35 am

    Note: I’m the EclipseLink JAXB (MOXy) lead and a member of the JAXB 2 (JSR-222) expert group.

    You could map your XML directly to your domain model using a JAXB implementation. JAXB requires Java SE 5, and a JAXB implementation is included in Java SE 6.

    Recon

    Your Recon class would look something like:

    package forum7673323;
    
    import java.io.File;
    import java.util.List;
    
    import javax.xml.bind.annotation.XmlAccessType;
    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlAttribute;
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlRootElement;
    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
    
    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Recon {
    
        @XmlAttribute
        private String outputPath;
    
        @XmlAttribute
        @XmlJavaTypeAdapter(FileAdapter.class)
        private File withCompareFile;
    
        @XmlAttribute
        @XmlJavaTypeAdapter(FileAdapter.class)
        private File toCompareFile;
    
        @XmlElement(name="sheet")
        private List<ReconSheet> reconSheets;
    
    }
    

    ReconSheet

    package forum7673323;
    
    import javax.xml.bind.annotation.XmlAccessType;
    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlAttribute;
    
    @XmlAccessorType(XmlAccessType.FIELD)
    public class ReconSheet {
    
        @XmlAttribute
        int sheetNumber;
    }
    

    FileAdapter

    Since a JAXB implementation can not interact with a java.io.File object directly we will use a JAXB adapter to handle this conversion. The use of this adapter is specified on the Recon class using the @XmlJavaTypeAdapter annotation:

    package forum7673323;
    
    import java.io.File;
    
    import javax.xml.bind.annotation.adapters.XmlAdapter;
    
    public class FileAdapter extends XmlAdapter <String, File>{
    
        @Override
        public String marshal(File file) throws Exception {
            if(null == file) {
                return null;
            }
            return file.getPath();
        }
    
        @Override
        public File unmarshal(String path) throws Exception {
            return new File(path);
        }
    
    }
    

    Demo

    package forum7673323;
    
    import java.io.File;
    
    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.Marshaller;
    import javax.xml.bind.Unmarshaller;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(Recon.class);
    
            File xml = new File("src/forum7673323/input.xml");
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            Recon recon = (Recon) unmarshaller.unmarshal(xml);
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(recon, System.out);
        }
    
    }
    

    Output

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <recon toCompareFile="h:\work\two.xls" withCompareFile="h:\work\one.xls" outputPath="h:/work">
        <sheet sheetNumber="1"/>
    </recon>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created XML file,but I can't view it/output it.I know there is no
friends, i have created custom title bar using following titlebar.xml file with code <?xml
I have created the following vsct file xml. <?xml version=1.0 encoding=utf-8?> <CommandTable xmlns=http://schemas.microsoft.com/VisualStudio/2005-10-18/CommandTable xmlns:xs=http://www.w3.org/2001/XMLSchema>
I have an XML document as follows: <directory> <file><monitored>0</monitored> <xferStatus>1</xferStatus> <name>test1.txt</name> <size>7</size> <created>03/31/10 11:30:02
I have created a XML image gallery, which displays text in between each slide.
I have looked at many examples for validating an XML file against a DTD,
I have created an .hbm.xml file : <xml version=1.0?> <hibernate-mapping package=com.cmp.iard.sinistre.hbm.dossier> <!DOCTYPE hibernate-mapping PUBLIC
I have created a XML file using PHP's simple XML, saved the file. When
I have a code to send XML via POST. But this code is in
i have created a template xml file witch contain some words like {contentname}. i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.