Programmer

追逐梦想的人
随笔 - 6, 文章 - 0, 评论 - 5, 引用 - 0
数据加载中……

java学习笔记(xml解析)

最近学习xml,把学习的代码发上来   希望对新手有用
这是note.xml
<?xml version="1.0" encoding="gb2312" ?> 
<notes>
<note date="2007-4-12">
<from>小红</from> 
<to>小林</to> 
<message>周末一起去吃火锅呀</message> 
</note>
</notes>

这是dom解析xml代码
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


class  DomXMLTest
{
    
public static void main(String[] args)
    {   
        
try{
         
//(1)得到DOM解析器的工厂实例
        DocumentBuilderFactory factory =DocumentBuilderFactory.newInstance();     
          
//(2)从DOM工厂获得DOM解析器
        DocumentBuilder  builder=factory.newDocumentBuilder(); 
        File f
=new File("note.xml");
         
//(3)把要解析的XML文档转化为输入流,以便DOM解析器解析它
        InputStream  is=new FileInputStream(f);  
        
//(4)解析XML文档的输入流,得到一个Document
        Document doc=builder.parse(is);    
        
//(5)得到XML文档的根节点
        Element  root=doc.getDocumentElement(); 
         
//(6)得到节点的子节点
        NodeList  notes=root.getChildNodes();   

          
for(int i=0;i<notes.getLength();i++)
          {
               Node note
=notes.item(i);
            
if(note.getNodeType()==Node.ELEMENT_NODE)
                {  
                    
//(7)取得节点的属性值
                    String date =note.getAttributes().getNamedItem("date").getNodeValue(); 
                    System.out.println(date);
                    
// (8)轮循子节点
                    for(Node node=note.getFirstChild();node!=null;node=node.getNextSibling()) 
                         {
                                
if(node.getNodeType()==Node.ELEMENT_NODE)
                                     {
                                            
if(node.getNodeName().equals("from"))
                                            {
                                                 String from
=node.getFirstChild().getNodeValue();
                                                 System.out.println(from);
                                             }
                                            
if(node.getNodeName().equals("to"))
                                             {
                                                  String to
=node.getFirstChild().getNodeValue();
                                                   System.out.println(to);
                                             }
                                             
if(node.getNodeName().equals("message"))
                                             {
                                                   String message
=node.getFirstChild().getNodeValue();
                                                   System.out.println(message);
                                              }
                                      }
                           }
                  }

        }
        }
        
catch(ParserConfigurationException e)
        {
            e.printStackTrace();
        }
        
catch(SAXException e)
        {
            e.printStackTrace();
        }
        
catch(IOException e)
        {
            e.printStackTrace();
        }
     }                                  
}

还有 出现 下面的错误  是xml的格式不对 ,我就应为在 <?xml 前面多个空格 就找了好几天的错误
特别感谢那些帮我找问题的高手,用范伟的话说  谢谢啊
The processing instruction target matching "[xX][mM][lL]" is not allowed.




posted on 2007-04-13 18:08 霜の哀伤 阅读(420) 评论(1)  编辑  收藏

评论

# re: java学习笔记(xml解析)  回复  更多评论   

ri , 找到问题方法 也不共享一下 干
2010-06-11 17:39 | dv

只有注册用户登录后才能发表评论。


网站导航: