aPugLife
aPugLife

Reputation: 1039

Java parse a xml with file drop

Having the filedrop already implemented in my code, I need to parse the xml file I drop in the main().

Main()

case "XML":
  text.append("Processing file type XML: "+files[i].getCanonicalPath() + "\n" );
  ReadXml read_xml = new ReadXml();
  read_xml.read(files[i].getCanonicalPath(), text);
  break;

ReadXml.java

public class ReadXml {

    ProgramDocument programDocument = new ProgramDocument();
    public void read(String FILE, javax.swing.JTextArea text ) {

    try {
        JAXBContext context = JAXBContext.newInstance(ProgramDocument.class);
        Unmarshaller u = context.createUnmarshaller();

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(FILE);
        Object o = u.unmarshal( doc );
        doc.getDocumentElement().normalize();
        text.append("Account : " +doc.getElementsByTagName("Account").item(0));

    }
    catch(Exception e) {
        text.append("XML file not parsed correctly.\n");
        }
    }
}

I am not able to print anything, and when I am, I see "NULL" or just empty row or some path@numbers

I am not a developer, I just need to try opening a xml a send contents to a DB, but this is too far already.

EDIT: added part of xml

<?xml version="1.0" encoding="UTF-8"?>
<ARRCD Version="48885" Release="38">
<Identification v="ORCOZIO"/>
<Version v="013"/>
<Account v="OCTO">
<Type v="MAJO"/>
<Date v="2016-05-14"/>
</AARCD>

Upvotes: 0

Views: 56

Answers (1)

Pierangelo Calanna
Pierangelo Calanna

Reputation: 572

There are no elements tagged "Account" in the element "Account". What you want to read here are the Attributes of Account, not other elements. Thus you should use eElement.getAttribute("v") if you want to read attribute v, not getElementsByTagName()

Upvotes: 2

Related Questions