孙鑫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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • java基础练习题(含答案)[通俗易懂]

    java基础练习题(含答案)[通俗易懂]7、数组操作题一现在有如下一个数组:intoldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5},要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为:intnewArr[]={1,3,4,5,6,6,5,4,7,6,7,5}intoldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5};intcount=0;for…

    2022年7月7日
    24
  • Jmeter正则表达式提取器获取Token-简单实例「建议收藏」

    Jmeter正则表达式提取器获取Token-简单实例「建议收藏」正则表达式提取响应数据中的Token步骤1:TestPlan下创建线程组,线程组下创建取样器->HTTP请求。上图中Login接口的Response中有用户token,该token需要在下一个需求提报的接口中使用,所以在Login下添加“正则表达式提取器”步骤2:选择Login,鼠标右键选择“添加->后置处理器->正则表达式提取器”步骤3:正则表达式…

    2022年9月4日
    3
  • es数据库简介

    es数据库简介##1.es是什么ElasticSearch简称ES,是一个高拓展和开源的全文搜索和分析引擎,可以准实时地存储、搜索、分析海量的数据。它和MongoDB、redis等一样是非关系型数据。业应用定位:采用RestfulAPI标准的可扩展和高可用的实时数据分析的全文搜索工具。可拓展:开源软件,支持很多第三方插件。高可用:在一个集群的多个节点中进行分布式存储,索引支持shards和复制,即使部分节点down掉,也能自动进行数据恢复和主从切换。采用RestfulAPI标准:通过http接口使用JSO

    2022年5月7日
    54
  • 什么是网站的灵魂_测试性能网站

    什么是网站的灵魂_测试性能网站前言在前一篇随笔《大型网站系统架构的演化》中,介绍了大型网站的演化过程,期间穿插了一些技术和手段,我们可以从中看出一个大型网站的轮廓,但想要掌握设计开发维护大型网站的技术,需要我们一步一步去研究实践

    2022年8月4日
    2
  • ros安装教程 ubuntu18.04_idea安装和配置教程

    ros安装教程 ubuntu18.04_idea安装和配置教程ROS是机器人操作系统(RobotOperatingSystem)的英文缩写,源自斯坦福的开源项目。Ubuntu16.04只能安装ROSLunarLoggerhead或ROSKineticKame依次完成以下四步.第四步提示是否不验证,需输入y,等待安装完成sudosh-c’echo”debhttp://packages.ros.org/ros/ubuntu$(lsb_release-sc)main”>/etc/apt/sources.list.d/r

    2022年9月10日
    0
  • 如何让你的网站支持 IE9 Pinned Site (Part 2 – 实战)

    如何让你的网站支持 IE9 Pinned Site (Part 2 – 实战)

    2021年8月8日
    48

发表回复

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

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