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


相关推荐

  • 银行风控模型

    银行风控模型风控催生原因对于银行来说,现今互联网贷款和信用卡办理面临的主要难题是数据和风控。站在银行或金融机构角度,自然而然是想获得更多的信息和数据,但是在收集数据这方面又是比较无力的。加上当下的发展趋势,消费贷以及贷款审批速度都要求快。如何在快的的过程中对客户进行一个全面的审查,得出一个合理的结果呢?如果没有详细的数据对客户进行评估,这势必会提高放贷的风险。风控概述所谓风控,是指多银行贷款资金的…

    2022年6月13日
    36
  • SQL中SELECT语句详解「建议收藏」

    SQL中SELECT语句详解「建议收藏」本篇文章讲述SQL语句中的SELECT查询语句,以供参考,如有错误或不当之处还望大神们告知。简单查询SELECT-FROM用于无条件查询单张表中的行或列假设有表如图所示查询名字叫‘叶清逸’的记录:select*fromT_USERwhereu_name=’叶清逸’;查询结果:查询一个或多个属性,u_name,u_age,u_scor…

    2022年4月29日
    56
  • 4-2 setting中一定要将ROBOTSTXT_OBEY = False的注释去掉[通俗易懂]

    4-2 setting中一定要将ROBOTSTXT_OBEY = False的注释去掉[通俗易懂]#Obeyrobots.txtrules##默认遵循robots协议的,默认去读取每个网站上的robots协议ROBOTSTXT_OBEY=False转载于:https://www.cnblogs.com/xudj/p/10163723.html

    2022年5月4日
    62
  • pycharm 2022.01.12 激活[最新免费获取]

    (pycharm 2022.01.12 激活)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html…

    2022年3月31日
    46
  • matlab interp1db,matlab – Matlab interp1图出现数据偏移 – 堆栈内存溢出

    matlab interp1db,matlab – Matlab interp1图出现数据偏移 – 堆栈内存溢出本质上,我正在尝试使用Matlab的interp1方法平滑图像分割中的轮廓线。不幸的是,interp1表现不佳,可能是因为我使用不正确。我的插值代码如下:y2=interp1(x,y,’nearest’);然后,我尝试将原始x值对y2以及原始函数作图(请参见附图)。plot(x,y2,’x’);我认为插值可以使原始函数平滑(在某种程度上可以做到),但是interp1方法似乎偏离…

    2022年6月7日
    29
  • MVC框架理解及优缺点

    MVC框架理解及优缺点

    2021年11月6日
    46

发表回复

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

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