简体   繁体   English

Java通过文件删除解析XML

[英]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(). 在我的代码中已经实现了filedrop之后,我需要解析放置在main()中的xml文件。

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 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 我无法打印任何内容,当我在打印时,我看到“ NULL”或只是空行或一些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. 我不是开发人员,我只需要尝试打开xml并将内容发送到DB,但这已经太过分了。

EDIT : added part of xml 编辑 :添加了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>

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() 因此,如果要读取属性v而不是getElementsByTagName(),则应使用eElement.getAttribute(“ v”)。

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

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