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

孙鑫XML视频教程中关于DOM例子的一点错误源代码如下: import java.io.File;import java.io.IOException;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationExcepti

大家好,又见面了,我是你们的朋友全栈君。

源代码如下:

 

孙鑫XML视频教程中关于DOM例子的一点错误
import
 java.io.File;
孙鑫XML视频教程中关于DOM例子的一点错误

import
 java.io.IOException;
孙鑫XML视频教程中关于DOM例子的一点错误
孙鑫XML视频教程中关于DOM例子的一点错误

import
 javax.xml.parsers.DocumentBuilder;
孙鑫XML视频教程中关于DOM例子的一点错误

import
 javax.xml.parsers.DocumentBuilderFactory;
孙鑫XML视频教程中关于DOM例子的一点错误

import
 javax.xml.parsers.ParserConfigurationException;
孙鑫XML视频教程中关于DOM例子的一点错误

import
 javax.xml.transform.Transformer;
孙鑫XML视频教程中关于DOM例子的一点错误

import
 javax.xml.transform.TransformerConfigurationException;
孙鑫XML视频教程中关于DOM例子的一点错误

import
 javax.xml.transform.TransformerException;
孙鑫XML视频教程中关于DOM例子的一点错误

import
 javax.xml.transform.TransformerFactory;
孙鑫XML视频教程中关于DOM例子的一点错误

import
 javax.xml.transform.dom.DOMSource;
孙鑫XML视频教程中关于DOM例子的一点错误

import
 javax.xml.transform.stream.StreamResult;
孙鑫XML视频教程中关于DOM例子的一点错误
孙鑫XML视频教程中关于DOM例子的一点错误

import
 org.w3c.dom.
*
;
孙鑫XML视频教程中关于DOM例子的一点错误

import
 org.xml.sax.SAXException;
孙鑫XML视频教程中关于DOM例子的一点错误
孙鑫XML视频教程中关于DOM例子的一点错误

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


{

孙鑫XML视频教程中关于DOM例子的一点错误
孙鑫XML视频教程中关于DOM例子的一点错误    
孙鑫XML视频教程中关于DOM例子的一点错误    
public static void main(String[] args)
孙鑫XML视频教程中关于DOM例子的一点错误孙鑫XML视频教程中关于DOM例子的一点错误    
{

孙鑫XML视频教程中关于DOM例子的一点错误        DocumentBuilderFactory dbf
=DocumentBuilderFactory.newInstance();
孙鑫XML视频教程中关于DOM例子的一点错误        
孙鑫XML视频教程中关于DOM例子的一点错误        
try
孙鑫XML视频教程中关于DOM例子的一点错误孙鑫XML视频教程中关于DOM例子的一点错误        
{

孙鑫XML视频教程中关于DOM例子的一点错误            DocumentBuilder db
=dbf.newDocumentBuilder();
孙鑫XML视频教程中关于DOM例子的一点错误            Document doc
=db.parse(new File(students.xml));
孙鑫XML视频教程中关于DOM例子的一点错误            
孙鑫XML视频教程中关于DOM例子的一点错误            Element eltStu
=doc.createElement(student);
孙鑫XML视频教程中关于DOM例子的一点错误            Element eltName
=doc.createElement(name);
孙鑫XML视频教程中关于DOM例子的一点错误            Element eltAge
=doc.createElement(age);
孙鑫XML视频教程中关于DOM例子的一点错误            
孙鑫XML视频教程中关于DOM例子的一点错误            Text txtName
=doc.createTextNode(王五);
孙鑫XML视频教程中关于DOM例子的一点错误            Text txtAge
=doc.createTextNode(19);
孙鑫XML视频教程中关于DOM例子的一点错误            
孙鑫XML视频教程中关于DOM例子的一点错误            eltName.appendChild(txtName);
孙鑫XML视频教程中关于DOM例子的一点错误            eltAge.appendChild(txtAge);
孙鑫XML视频教程中关于DOM例子的一点错误            
孙鑫XML视频教程中关于DOM例子的一点错误            eltStu.appendChild(eltName);
孙鑫XML视频教程中关于DOM例子的一点错误            eltStu.appendChild(eltAge);
孙鑫XML视频教程中关于DOM例子的一点错误            
孙鑫XML视频教程中关于DOM例子的一点错误            eltStu.setAttribute(
sn,03);
孙鑫XML视频教程中关于DOM例子的一点错误            
孙鑫XML视频教程中关于DOM例子的一点错误            Element root
=doc.getDocumentElement();
孙鑫XML视频教程中关于DOM例子的一点错误            root.appendChild(eltStu);
孙鑫XML视频教程中关于DOM例子的一点错误            
孙鑫XML视频教程中关于DOM例子的一点错误            NodeList nl
=root.getElementsByTagName(student);
孙鑫XML视频教程中关于DOM例子的一点错误            root.removeChild(nl.item(
0));
孙鑫XML视频教程中关于DOM例子的一点错误            
孙鑫XML视频教程中关于DOM例子的一点错误            Element eltStuChg
=(Element)nl.item(0);
孙鑫XML视频教程中关于DOM例子的一点错误            Node nodeAgeChg
=eltStuChg.getElementsByTagName(age).item(0);
孙鑫XML视频教程中关于DOM例子的一点错误            nodeAgeChg.getFirstChild().setNodeValue(
22);
孙鑫XML视频教程中关于DOM例子的一点错误            
孙鑫XML视频教程中关于DOM例子的一点错误            
int len=nl.getLength();
孙鑫XML视频教程中关于DOM例子的一点错误            
for(int i=0;i<len;i++)
孙鑫XML视频教程中关于DOM例子的一点错误孙鑫XML视频教程中关于DOM例子的一点错误            
{

孙鑫XML视频教程中关于DOM例子的一点错误                Element elt
=(Element)nl.item(i);
孙鑫XML视频教程中关于DOM例子的一点错误                System.out.println(
编号: +elt.getAttribute(sn));
孙鑫XML视频教程中关于DOM例子的一点错误                
孙鑫XML视频教程中关于DOM例子的一点错误                Node nodeName
=elt.getElementsByTagName(name).item(0);
孙鑫XML视频教程中关于DOM例子的一点错误                Node nodeAge
=elt.getElementsByTagName(age).item(0);
孙鑫XML视频教程中关于DOM例子的一点错误                
孙鑫XML视频教程中关于DOM例子的一点错误                String name
=nodeName.getFirstChild().getNodeValue();
孙鑫XML视频教程中关于DOM例子的一点错误                String age
=nodeAge.getFirstChild().getNodeValue();
孙鑫XML视频教程中关于DOM例子的一点错误                
孙鑫XML视频教程中关于DOM例子的一点错误                System.out.println(
姓名: +name);
孙鑫XML视频教程中关于DOM例子的一点错误                System.out.println(
年龄: +age);
孙鑫XML视频教程中关于DOM例子的一点错误                
孙鑫XML视频教程中关于DOM例子的一点错误                System.out.println(
—————————–);
孙鑫XML视频教程中关于DOM例子的一点错误            }

孙鑫XML视频教程中关于DOM例子的一点错误            
孙鑫XML视频教程中关于DOM例子的一点错误            TransformerFactory tff
=TransformerFactory.newInstance();
孙鑫XML视频教程中关于DOM例子的一点错误            Transformer tf
=tff.newTransformer();
孙鑫XML视频教程中关于DOM例子的一点错误            tf.setOutputProperty(
encoding,gb2312);
孙鑫XML视频教程中关于DOM例子的一点错误            DOMSource source
=new DOMSource(doc);
孙鑫XML视频教程中关于DOM例子的一点错误            StreamResult result
=new StreamResult(new File(converted.xml));
孙鑫XML视频教程中关于DOM例子的一点错误            tf.transform(source,result);
孙鑫XML视频教程中关于DOM例子的一点错误        }

孙鑫XML视频教程中关于DOM例子的一点错误        
catch (ParserConfigurationException e)
孙鑫XML视频教程中关于DOM例子的一点错误孙鑫XML视频教程中关于DOM例子的一点错误        
{

孙鑫XML视频教程中关于DOM例子的一点错误            
// TODO 自动生成 catch 块
孙鑫XML视频教程中关于DOM例子的一点错误
            e.printStackTrace();
孙鑫XML视频教程中关于DOM例子的一点错误        }

孙鑫XML视频教程中关于DOM例子的一点错误        
catch (SAXException e)
孙鑫XML视频教程中关于DOM例子的一点错误孙鑫XML视频教程中关于DOM例子的一点错误        
{

孙鑫XML视频教程中关于DOM例子的一点错误            
// TODO 自动生成 catch 块
孙鑫XML视频教程中关于DOM例子的一点错误
            e.printStackTrace();
孙鑫XML视频教程中关于DOM例子的一点错误        }

孙鑫XML视频教程中关于DOM例子的一点错误        
catch (IOException e)
孙鑫XML视频教程中关于DOM例子的一点错误孙鑫XML视频教程中关于DOM例子的一点错误        
{

孙鑫XML视频教程中关于DOM例子的一点错误            
// TODO 自动生成 catch 块
孙鑫XML视频教程中关于DOM例子的一点错误
            e.printStackTrace();
孙鑫XML视频教程中关于DOM例子的一点错误        }

孙鑫XML视频教程中关于DOM例子的一点错误        
catch (TransformerConfigurationException e)
孙鑫XML视频教程中关于DOM例子的一点错误孙鑫XML视频教程中关于DOM例子的一点错误        
{

孙鑫XML视频教程中关于DOM例子的一点错误            
// TODO 自动生成 catch 块
孙鑫XML视频教程中关于DOM例子的一点错误
            e.printStackTrace();
孙鑫XML视频教程中关于DOM例子的一点错误        }

孙鑫XML视频教程中关于DOM例子的一点错误        
catch (TransformerException e)
孙鑫XML视频教程中关于DOM例子的一点错误孙鑫XML视频教程中关于DOM例子的一点错误        
{

孙鑫XML视频教程中关于DOM例子的一点错误            
// TODO 自动生成 catch 块
孙鑫XML视频教程中关于DOM例子的一点错误
            e.printStackTrace();
孙鑫XML视频教程中关于DOM例子的一点错误        }

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

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


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

 其中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));

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/145768.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • mac时间机器删除旧备份

    mac时间机器删除旧备份

    2021年5月14日
    116
  • 软件测试报告如何写好_软件功能测试报告模板

    软件测试报告如何写好_软件功能测试报告模板入行软件测试的人员最需要掌握的基本功有三:设计测试用例、发现缺陷、撰写测试报告,透过这三个基本功基本可以摸清一名测试人员的专业度及其在其他方面的测试技能熟练程度,而从测试报告可以看出用例设计和发现缺陷

    2022年8月3日
    5
  • python 字典最外层使用_python字典底层实现

    python 字典最外层使用_python字典底层实现前言问题1:python中的字典到底是有序还是无序问题2:python中字典的效率如何python字典底层原理在Python3.5以前,字典是不能保证顺序的,键值对A先插入字典,键值对B后插

    2022年7月31日
    6
  • socket粘包解决方案_socket 传输文件

    socket粘包解决方案_socket 传输文件一 .两个简单概念长连接与短连接:1.长连接   Client方与Server方先建立通讯连接,连接建立后不断开, 然后再进行报文发送和接收。2.短连接   Client方与Server每进行一次报文收发交易时才进行通讯连接,交易完毕后立即断开连接。此种方式常用于一点对多点通讯,比如多个Client连接一个Server.二 ….

    2022年8月18日
    7
  • windows安装深度linux,最漂亮的国产Linux,windows下安装深度操作系统步骤

    windows安装深度linux,最漂亮的国产Linux,windows下安装深度操作系统步骤GIF国产操作系统都是基于Linux进行的二次开发,有很多国产系统只是在Linux基础上进行一些美化、内置几个软件就号称自己是操作系统了。而为什么深度操作系统deepinLinux一直深受用户喜爱呢?虽然它也是基于Linux内核,但它的整个系统架构设计都是自己研发制作的。从显示管理器、资源管理器再到桌面环境及比较实用的深度全家桶,在这个系统上,你不仅可以Linux原生的软件,还可以使用QQ、TI…

    2022年5月13日
    66
  • 百度云网页视频加速播放

    百度云网页视频加速播放最近使用百度云看考研视频,但是百度云的播放器太简陋,无法倍速播放视频,太浪费时间了,听说firefox可以加速播放,可我又是chrome的死忠,于是在网上查到一个简单的方法。进入播放页面,按F12进入开发者模式在console中输入:videojs.getPlayers(&quot;video-player&quot;).html5player.tech_.setPlaybackRate(2…

    2022年5月24日
    39

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号