孙鑫XML视频教程中关于DOM例子的一点错误

源代码如下:

 

import  java.io.File;
import  java.io.IOException;

import  javax.xml.parsers.DocumentBuilder;
import  javax.xml.parsers.DocumentBuilderFactory;
import  javax.xml.parsers.ParserConfigurationException;
import  javax.xml.transform.Transformer;
import  javax.xml.transform.TransformerConfigurationException;
import  javax.xml.transform.TransformerException;
import  javax.xml.transform.TransformerFactory;
import  javax.xml.transform.dom.DOMSource;
import  javax.xml.transform.stream.StreamResult;

import  org.w3c.dom. * ;
import  org.xml.sax.SAXException;

public   class  DOMConvert
{

    
    
public static void main(String[] args)
    
{
        DocumentBuilderFactory dbf
=DocumentBuilderFactory.newInstance();
        
        
try
        
{
            DocumentBuilder db
=dbf.newDocumentBuilder();
            Document doc
=db.parse(new File("students.xml"));
            
            Element eltStu
=doc.createElement("student");
            Element eltName
=doc.createElement("name");
            Element eltAge
=doc.createElement("age");
            
            Text txtName
=doc.createTextNode("王五");
            Text txtAge
=doc.createTextNode("19");
            
            eltName.appendChild(txtName);
            eltAge.appendChild(txtAge);
            
            eltStu.appendChild(eltName);
            eltStu.appendChild(eltAge);
            
            eltStu.setAttribute(
"sn","03");
            
            Element root
=doc.getDocumentElement();
            root.appendChild(eltStu);
            
            NodeList nl
=root.getElementsByTagName("student");
            root.removeChild(nl.item(
0));
            
            Element eltStuChg
=(Element)nl.item(0);
            Node nodeAgeChg
=eltStuChg.getElementsByTagName("age").item(0);
            nodeAgeChg.getFirstChild().setNodeValue(
"22");
            
            
int len=nl.getLength();
            
for(int i=0;i<len;i++)
            
{
                Element elt
=(Element)nl.item(i);
                System.out.println(
"编号: "+elt.getAttribute("sn"));
                
                Node nodeName
=elt.getElementsByTagName("name").item(0);
                Node nodeAge
=elt.getElementsByTagName("age").item(0);
                
                String name
=nodeName.getFirstChild().getNodeValue();
                String age
=nodeAge.getFirstChild().getNodeValue();
                
                System.out.println(
"姓名: "+name);
                System.out.println(
"年龄: "+age);
                
                System.out.println(
"-----------------------------");
            }

            
            TransformerFactory tff
=TransformerFactory.newInstance();
            Transformer tf
=tff.newTransformer();
            tf.setOutputProperty(
"encoding","gb2312");
            DOMSource source
=new DOMSource(doc);
            StreamResult result
=new StreamResult(new File("converted.xml"));
            tf.transform(source,result);
        }

        
catch (ParserConfigurationException e)
        
{
            
// TODO 自动生成 catch 块
            e.printStackTrace();
        }

        
catch (SAXException e)
        
{
            
// TODO 自动生成 catch 块
            e.printStackTrace();
        }

        
catch (IOException e)
        
{
            
// TODO 自动生成 catch 块
            e.printStackTrace();
        }

        
catch (TransformerConfigurationException e)
        
{
            
// TODO 自动生成 catch 块
            e.printStackTrace();
        }

        
catch (TransformerException e)
        
{
            
// TODO 自动生成 catch 块
            e.printStackTrace();
        }

    }


}

 其中StreamResult result=new StreamResult(new File("converted.xml"));这是错误的,如果这么写的话,会
有类似如下的异常

java.io.FileNotFoundException: file:/E:/我的项目/DomStudy/src/dom/converted.xml (文件名、目录名或卷标语法不正确。)

我们应该这样写:StreamResult result=new StreamResult(new FileOutputStream("converted.xml", false));

你可能感兴趣的:(xml,String,File,Class,import,encoding)