首页 新闻 会员 周边 捐助

请大神看一眼帮帮忙,非常感谢,xml通过W3C解析为什么总是返回[#document:null]

0
悬赏园豆:50 [待解决问题]

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();  }

}

JAVA_Viter的主页 JAVA_Viter | 初学一级 | 园豆:158
提问于:2015-02-26 11:55
< >
分享
所有回答(2)
0

没准问题出现在text.xml上面呢,把这个文件的内容页放上来吧

刘宏玺 | 园豆:14020 (专家六级) | 2015-02-26 13:01

<?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>

支持(0) 反对(0) JAVA_Viter | 园豆:158 (初学一级) | 2015-02-26 13:29
0

File f = new File("test.xml");  

有没有办法看看这个f是否为null?

爱编程的大叔 | 园豆:30844 (高人七级) | 2015-02-26 15:32

System.out.println(f.exists());  // result : true

支持(0) 反对(0) JAVA_Viter | 园豆:158 (初学一级) | 2015-02-27 09:34
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册