import java.io.File; import java.io.IOException;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document; import org.w3c.dom.Element; import org.xml.sax.SAXException;
public class ReadXML {
public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
File f = new File("test.xml");
Document doc = db.parse(f); // text.xml 是有内容的,为什么返回的总是[#document:null],获取不到结点内容呢?
Element e = doc.getDocumentElement();
System.out.println(doc.getTextContent());
System.out.println(doc.getNodeName());
System.out.println(doc.getNodeValue());
System.out.println(doc.getNodeType());
org.w3c.dom.Element root = doc.getDocumentElement(); }
}
没准问题出现在text.xml上面呢,把这个文件的内容页放上来吧
<?xml version="1.0" encoding="UTF-8"?>
<object-type type="APP_VM" tagname="ecVVM" xmlns="http://www.ecv.com/BD_EXPORT">
<bd-objects>
<bd-object>
<fields>
<field field-id="appvmAppVmId" tagname="ecVisionVMKey"></field>
<field field-id="appvmVersionNo" tagname="DocumentEdition"></field>
<field field-id="appvmCode" tagname="VendorCode"></field>
<field field-id="appvmEnglishName" tagname="VendorEnglishName"></field>
<field field-id="appvmNickName" tagname="VendorNickName"></field>
<field field-id="appvmIDHK" tagname="HKID"></field>
<field field-id="appvmIDChile" tagname="ChileID"></field>
<field field-id="appvmIDPeru" tagname="PeruID"></field>
<field field-id="appvmIDColombia" tagname="ColombiaID"></field>
</fields>
<bd-subobjects>
<object-type type="MM_VM_ADDR" tagname="ecVVMAddr">
<bd-objects>
<bd-object>
<fields>
<field field-id="appvmAddrId" tagname="ecVVMAddrKey"></field>
<field field-id="appvmAddrAppVmId" tagname="ecVisionVMAddrParentKey"></field>
<field field-id="appvmAddressCode" tagname="Address"></field>
<field field-id="appvmAddrCity" tagname="City"></field>
<field field-id="appvmState" tagname="State"></field>
<field field-id="appvmZIP" tagname="ZIP"></field>
</fields>
</bd-object>
</bd-objects>
</object-type>
<object-type type="APP_VM_BANK" tagname="ecVisionVMBank">
<bd-objects>
<bd-object>
<fields>
<field field-id="appvmBankId" tagname="ecVisionVMAddrKey"></field>
<field field-id="appvmBankAppVmId" tagname="ecVisionVMAddrParentKey"></field>
<field field-id="appvmAdvisingBank" tagname="AdvisingBank"></field>
<field field-id="appvmBeneficiaryName" tagname="BeneficiaryName"></field>
<field field-id="appvmBeneficiaryAddress" tagname="BeneficiaryAddress"></field>
</fields>
</bd-object>
</bd-objects>
</object-type>
</bd-subobjects>
</bd-object>
</bd-objects>
</object-type>
File f = new File("test.xml");
有没有办法看看这个f是否为null?
System.out.println(f.exists()); // result : true