Java使用文件拖放解析xml
在代码中已经实现了filedrop之后,我需要解析在main()中放入的xml文件 Main() ReadXml.javaJava使用文件拖放解析xml,java,xml,unmarshalling,Java,Xml,Unmarshalling,在代码中已经实现了filedrop之后,我需要解析在main()中放入的xml文件 Main() ReadXml.java public class ReadXml { ProgramDocument programDocument = new ProgramDocument(); public void read(String FILE, javax.swing.JTextArea text ) { try { JAXBContext context
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");
}
}
}
我无法打印任何内容,当我打印时,我会看到“NULL”或只是空行或一些path@numbers
我不是开发人员,我只需要尝试打开一个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>
元素“Account”中没有标记为“Account”的元素。 这里您要阅读的是Account的属性,而不是其他元素。
因此,如果要读取属性v,而不是getElementsByTagName(),则应该使用eeElement.getAttribute(“v”),因为元素“Account”中没有标记为“Account”的元素。 这里您要阅读的是Account的属性,而不是其他元素。 因此,如果要读取属性v,而不是getElementsByTagName(),则应该使用eeElement.getAttribute(“v”)
<?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>