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


相关推荐

  • PyCharm官网无法访问的解决办法[通俗易懂]

    PyCharm官网无法访问的解决办法[通俗易懂]问题描述  最近在研究pythonweb框架,用的是以前学python的时候用的Pycharm社区版(无力吐槽)。不太好用,就想去下个企业版用用,结果出现这种情况。。。  emmm,检查了网络没问题,换了个浏览器也是无法访问,最后辗转多个论坛发现似乎是因为hosts文件的问题。模糊的记得我好像激活成功教程过这个软件修改了一下hosts文件,改回来就可以访问Pycharm官网了。解决办法  Wi…

    2022年8月26日
    4
  • 网络号 子网号 主机号「建议收藏」

    网络号 子网号 主机号「建议收藏」网络号子网号主机号第一个例子:已知 IP:192.169.20.50   子网掩码:255.255.255.224  求网络号 子网号 主机号。首先子网掩码255.255.255.224转换为二进制位255.255.255.224:11111111.11111111.11111111.11100000可以看到这个掩码的左边三节与C类默认掩码相同,只有第四节与C类默认掩码不 同,

    2022年6月24日
    29
  • 十以内的加法游戏_10以内加减法游戏

    十以内的加法游戏_10以内加减法游戏现在市面上有非常多的益智类小游戏,其中就有很多是关于速算问题的,我们可以通过scratch编程来实现一个更加具有画面感的速算类小游戏,来帮助有计算问题的小朋友通过游戏来进行更好的心算速算练习。现在的这一款10以内的加法速算游戏,比较适合刚接触计算年龄比较小的孩子。1:首先我们需要添加几个有趣的角色。2,添加背景并设置好需要的变量。3,利用“询问”指令来实现出题的效果,并通过判断”回答”是…

    2025年5月31日
    1
  • 联合索引在B+Tree上的存储结构及数据查找方式[通俗易懂]

    联合索引在B+Tree上的存储结构及数据查找方式[通俗易懂]最困难的事情就是认识自己!个人网站,欢迎访问!前言:本篇文章主要是阐述下联合索引在B+Tree上的实际存储结构。本文主要讲解的内容有:联合索引在B+树上的存储结构联合索引的查找方式为什么会有最左前缀匹配原则在分享这篇文章之前,我在网上查了关于MySQL联合索引在B+树上的存储结构这个问题,翻阅了很多博客和技术文章,其中有几篇讲述的与事实相悖。具体如下:很多博客中都是说:联合索引在B+树上的非叶子节点中只会存储联合索引中的第一个索引字段的.

    2022年6月4日
    27
  • BaseAdapter的notifyDataSetChanged方法[通俗易懂]

    BaseAdapter的notifyDataSetChanged方法[通俗易懂]都用过BaseAdapter的notifyDataSetChanged()方法,用法很简单,当BaseAdapter的数据更新了,需要更改显示,这时候就要调用notifyDataSetChanged()方法来更新数据,当然你可以用一种比较恶心的方式,在你所使用的AdapterView(这里是指AdapterView的子类,ListView,GridView,Gallery等等),调setAdap

    2022年6月24日
    38
  • java tess4j ddl,分配为war NoClassDefFoundError后出现Tess4J错误:无法初始化类net.sourceforge.tess4j.TessAPI…「建议收藏」

    java tess4j ddl,分配为war NoClassDefFoundError后出现Tess4J错误:无法初始化类net.sourceforge.tess4j.TessAPI…「建议收藏」IhaveSpringbootWebserverprojectwhichworksokinmyPCunderIntellijIDEA,butitnotworksafterdistributingtothesamePCaswarfile-NoClassDefFoundError:Couldnotinitializeclassnet…

    2022年5月10日
    39

发表回复

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

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