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


相关推荐

  • 用混淆矩阵计算kappa系数「建议收藏」

    用混淆矩阵计算kappa系数「建议收藏」从一篇论文——融合注意力机制和高效网络的糖尿病视网膜病变识别与分类,看到人家除了特异性、敏感性、准确率、混淆矩阵以外,还用了加权kappa系数,所以了解一下kapp系数的知识,加权kappa还没找到更好的资料。。。资料来源于百度百科词条——kappa系数Kappa系数用于一致性检验,也可以用于衡量分类精度,但kappa系数的计算是基于混淆矩阵的.kappa系数是一种衡量分类精度的指标。它是通过把所有地表真实分类中的像元总数(N)乘以混淆矩阵对角线(Xkk)的和,再减去某一类地表真实像元总数与该类中被

    2022年6月28日
    27
  • WireShark抓包后数据分析

    WireShark抓包后数据分析在分析数据之前,我们先了解一下我们传输数据的结构体系,如下图:这是两种体系,我们常知的一般都是TCP/IP体系结构。TCP/IP体系架构分析不难发现,TCP/IP体系中包含着很多我们熟悉的协议,比如说:http、smtp、https等。而我们人(使用者)是站在应用层之上的,我们想把数据上传或者说发送给别人,就要通过一些应用,如:QQ、微信、百度网盘等。然后就经过一层层加密(在数据包前加个“头”),一层层的传递。Frame层(物理层)分析这是我自己抓包的一个例子,我从我自己的QQ发了一条消息给朋友,

    2022年9月4日
    5
  • 拉格朗日乘子法以及KKT条件

    拉格朗日乘子法以及KKT条件

    2021年11月19日
    53
  • linux无限刷屏代码,linux下如何实现简单刷屏

    linux无限刷屏代码,linux下如何实现简单刷屏今天为了测试我的LCD有没有坏点写了一个简单的刷屏程序,效果还不错,分析给大家。代码如下:#include#include#include#include#include#include#include#includeintmain(intargc,char*argv[]){intfbfd=0;structfb_var_screeninfovinfo;struct…

    2022年5月4日
    152
  • 关于红黑树的学习笔记

    关于红黑树的学习笔记

    2021年6月14日
    101
  • 彻底卸载pycharm 恢复环境

    彻底卸载pycharm 恢复环境彻底卸载pycharm配置文件恢复最初环境

    2022年8月28日
    1

发表回复

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

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