java try() 变量_在Java主函数中,为什么try中的变量不能在try之外被调用

publicstaticvoidmain(String[]args){Filefile1=newFile("H:\\输入的XML文件.xml");DocumentBuilderFactorydbf=DocumentBuilderFactory.newInstance();try{DocumentBuilderdb=dbf.new...

public static void main(String[] args){

File file1 = new File("H:\\输入的XML文件.xml");

DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();

try{

DocumentBuilder db=dbf.newDocumentBuilder(); Document doc=db.parse(file1);

Element root=doc.getDocumentElement();

}catch(SAXException se){

//解析过程错误

Exception e=se;

if(se.getException()!=null)

e=se.getException();

e.printStackTrace();

}catch(ParserConfigurationException pe){

//解析器设定错误

pe.printStackTrace();

}catch(IOException ie){

//文件处理错误

ie.printStackTrace();

}

我现在要定义一个String变量root1 : root1=root.getTagName();

但是编译器提示root没有定义啊。我已经在try中定义了root,但是为什么不好使,该怎么办。我对Java不是很熟,知道的解释一下,谢谢了

展开

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值