xml bianji

来源:互联网 发布:网络机顶盒软件app 编辑:程序博客网 时间:2024/06/05 15:09

这是删除接点的例子

import   java.awt.*;
import   java.awt.event.*;
import   org.apache.xerces.parsers.DOMParser;
import   org.xml.sax.SAXException;
import     java.awt.*;    
import     java.awt.event.*;    
import     org.apache.xerces.parsers.DOMParser;    
import     org.xml.sax.SAXException;    
import     org.w3c.dom.Document;    
import     org.w3c.dom.Node;    
import     org.w3c.dom.NodeList;    
import     java.io.IOException;    
import     org.w3c.dom.NamedNodeMap;    
 
import     java.io.*;    
import     org.w3c.dom.*;    
import     org.apache.xerces.dom.*;    
import     org.apache.xml.serialize.*;    
import     org.apache.xerces.parsers.DOMParser;    
import     javax.xml.parsers.DocumentBuilder;    
import     javax.xml.parsers.DocumentBuilderFactory;    
 
 
class     TjNode     extends     Frame     {
   
        public     static     void     main(String     args[])     {                                                  
              DocumentBuilder     parser;    
              DocumentBuilderFactory     factory     =     DocumentBuilderFactory.newInstance();                
             
              try{
               
                      parser     =     factory.newDocumentBuilder();    
                      Document     doc     =     parser.parse( "system_file.xml ");                            
                      NodeList     newKey=doc.getElementsByTagName( "Name ");    
                      for(int     i=0;i <newKey.getLength();i++){    
                        Node     aNode=newKey.item(i);    
                        String     newString=     aNode.getFirstChild().getNodeValue().toString();    
                        System.out.println(newString);                                          
                        if(newString.equals( "dfg ")){      
                          aNode.getParentNode().removeChild(aNode);    
                        }    
                    }    
                      File     _outputFile=new     File( "system_file.xml ");    
                      OutputFormat     outputFormat=new     OutputFormat( "XML ", "gb2312 ",true);    
                      FileWriter     fileWriter=new     FileWriter(_outputFile);    
                      XMLSerializer     xmlSerializer=new     XMLSerializer(fileWriter,outputFormat);    
                      xmlSerializer.asDOMSerializer();    
                      xmlSerializer.serialize(doc.getDocumentElement());    
              }catch     (Exception     evc){    
                      System.out.println(evc.toString());    
              }    
        }    
}

这是更新的例子

DocumentBuilder   parser;
        DocumentBuilderFactory   factory   =   DocumentBuilderFactory.newInstance();
        try{
            parser   =   factory.newDocumentBuilder();
            Document   doc   =   parser.parse( "system_file.xml ");
            Element   Name=doc.createElement( "Name ");
            Name.setAttribute( "savepath ",pathText.getText());
            Name.setAttribute( "savesystem ",system_String);
            Name.appendChild(doc.createTextNode(treenameText.getText()));
       
            doc.getDocumentElement().appendChild(Name);
            File   _outputFile=new   File( "system_file.xml ");
            OutputFormat   outputFormat=new   OutputFormat( "XML ", "gb2312 ",true);
            FileWriter   fileWriter=new   FileWriter(_outputFile);
            XMLSerializer   xmlSerializer=new   XMLSerializer(
                    fileWriter,outputFormat);
            xmlSerializer.asDOMSerializer();
            xmlSerializer.serialize(doc.getDocumentElement());
            getnewtreeNode=treenameText.getText();
        }catch   (Exception   evc){
            System.out.println(evc.toString());
        }

写xml一样看看就可以了。

这些包在jbuilder内都有。
或者去http://jakarta.apache.org下载

这个例子是把xml文件变成javabean

import   org.apache.xerces.parsers.SAXParser;
import   org.xml.sax.InputSource;
import   java.io.FileInputStream;
import   java.util.Hashtable;
import   java.util.Enumeration;
import   org.xml.sax.helpers.DefaultHandler;
import   org.xml.sax.Attributes;

class   Employee{

private   String   id= " ";
private   Profile   profile=null;

public   void   setId(String   newId){
id=newId;
}

public   String   getId(){
return   id;
}

public   void   setProfile(Profile   newProfile){
profile=newProfile;
}

public   String   toString(){
return   "Employee:   "+id+ "/n "
+profile.getFirstName()+ "/n "
+profile.getLastName();
}
}

class   Profile{

String   firstName= " ";
String   lastName= " ";

public   void   setFirstName(String   newFirstName){
firstName=newFirstName;
}

public   String   getFirstName(){
return   firstName;
}

public   void   setLastName(String   newLastName){
lastName=newLastName;
}
public   String   getLastName(){
return   lastName;
}
}

class   EmployeeBuilder   extends   DefaultHandler{

private   Hashtable   employeeList=null;
private   Employee   employee=null;
private   Profile   profile=null;

public   void   startDocument(){

employeeList=new   Hashtable();
}

public   void   endElement(String   nameSpaceURI,String   name,
String   qName){

if(name.equals( "employee ")){
employeeList.put(employee.getId(),employee);
}
}

public   void   startElement(String   namespaceURI,String   name,
String   qName,Attributes   atts){

if(name.equals( "employee ")){
employee=new   Employee();
employee.setId(atts.getValue( "id "));
}

if(name.equals( "profile ")){
profile=new   Profile();

profile.setFirstName(atts.getValue( "firstName "));
profile.setLastName(atts.getValue( "lastName "));
employee.setProfile(profile);
}
}

public   Hashtable   getEmployess(){
return   employeeList;
}
}

class   SAXEmployeeExample1{

public   static   void   main(String   args[]){

EmployeeBuilder   eBuilder=new   EmployeeBuilder();

try{
SAXParser   saxParser=new   SAXParser();
saxParser.setContentHandler(eBuilder);
saxParser.parse(new   InputSource(
new   FileInputStream( "employees.xml ")));
Hashtable   employees=eBuilder.getEmployess();
Enumeration   emps=employees.elements();
while(emps.hasMoreElements()){
Employee   employee=(Employee)emps.nextElement();
System.out.println(employee);
}
}catch(Exception   ex){
System.out.println(ex);
}
}
}

xml文件

<?xml   version= "1.0 "?>
<employees>
<employee   id= "A124-567 ">
<profile   firstName= "Joe "   lastName= "Coder "/>
</employee>
<employee   id= "A1234-789 ">
<profile   firstName= "Danny "   lastName= "shi "/>
</employee>
</employees>


由于不是很清楚你的想法,所有多写了点.

 

dom解析,用
try{
            DocumentBuilderFactory   factory=DocumentBuilderFactory.newInstance();
            DocumentBuilder   builder=factory.newDocumentBuilder();
            doc=builder.newDocument();
   
            //获得TopicID
            int     i     =   TaminoWrapper.GetUniqueID(dbURL);


            TopicID=String.valueOf(i);
            ParentID=TopicID;

            Element   root=doc.createElement( "TOPIC ");
            Element   Issue1=doc.createElement( "Issue ");

            Element   RootID1=doc.createElement( "RootID ");
            RootID1.appendChild(doc.createTextNode( "100 "));
            Issue1.appendChild(RootID1);


            Element   ParentID1=doc.createElement( "ParentID ");
            ParentID1.appendChild(doc.createTextNode(ParentID));
            Issue1.appendChild(ParentID1);

            Element   TopicName1=doc.createElement( "TopicName ");
            TopicName1.appendChild(doc.createTextNode(TopicName));
            Issue1.appendChild(TopicName1);

            Element   AuthorName1=doc.createElement( "AuthorName ");
            AuthorName1.appendChild(doc.createTextNode(AuthorName));
            Issue1.appendChild(AuthorName1);


         
            Element   TopicID1=doc.createElement( "TopicID ");
            TopicID1.appendChild(doc.createTextNode(TopicID));
            Issue1.appendChild(TopicID1);


            Element   AuthorID1=doc.createElement( "AuthorID ");
            AuthorID1.appendChild(doc.createTextNode( "50 "));
            Issue1.appendChild(AuthorID1);

            Element   content1=doc.createElement( "content ");
            content1.appendChild(doc.createTextNode(content));
            Issue1.appendChild(content1);

            Element   upDateTime1=doc.createElement( "upDateTime ");
            upDateTime1.appendChild(doc.createTextNode(upDateTime));
            Issue1.appendChild(upDateTime1);
 

            Element   ReplyTimes1=doc.createElement( "ReplyTimes ");
            ReplyTimes1.appendChild(doc.createTextNode( "0 "));
            Issue1.appendChild(ReplyTimes1);

            Element   Mark1=doc.createElement( "Mark ");
            Mark1.appendChild(doc.createTextNode(Mark));
            Issue1.appendChild(Mark1);

            Element   BBSname1=doc.createElement( "BBSname ");
            BBSname1.appendChild(doc.createTextNode(BBSname));
            Issue1.appendChild(BBSname1);
   
          Element   ViewTimes1=doc.createElement( "ViewTimes ");
            ViewTimes1.appendChild(doc.createTextNode( "0 "));
            Issue1.appendChild(ViewTimes1);

         
            Element   SignName1=doc.createElement( "SignName ");
            String   Value=getUser.getUser( "SignName ",AuthorName);
            SignName1.appendChild(doc.createTextNode(Value));
            Issue1.appendChild(SignName1);

            Element   Photo1=doc.createElement( "Photo ");
            Value=getUser.getUser( "Photo ",AuthorName);
            Photo1.appendChild(doc.createTextNode(Value));
            Issue1.appendChild(Photo1);
 
            Element   NickName1=doc.createElement( "NickName ");
            Value=getUser.getUser( "NickName ",AuthorName);
            NickName1.appendChild(doc.createTextNode(Value));
            Issue1.appendChild(NickName1);

            Element   Rank1=doc.createElement( "Rank ");
            Value=getUser.getUser( "Rank ",AuthorName);
            Rank1.appendChild(doc.createTextNode(Value));
            Issue1.appendChild(Rank1);

            Element   BBSID1=doc.createElement( "BBSID ");
            BBSID1.appendChild(doc.createTextNode(BBSID));
            Issue1.appendChild(BBSID1);
 
            Element   SortTime1=doc.createElement( "SortTime ");
            SortTime1.appendChild(doc.createTextNode(SortTime));
            Issue1.appendChild(SortTime1);

            root.appendChild(Issue1);
            doc.appendChild(root);

   
            try{
                TaminoWrapper.Process(dbURL,doc, "BBS_topic ");
            }
            catch(Exception   ee)
                    {out.println( "提交失败 ");return;
            }

        }
    }
        catch(Exception   ex)
                {out.println( "用户名错误 ");
   
        }
类似我这样的代码可以把xml树组在内存里

 

两位前辈:
写xml目前已经不是我的问题了,关于那个包,我同学给我提供了jbuild下的一个叫xer**的包
用dom方法也把xml写出来了,但是现在我写的这个xml有个问题,就是我加属性的顺序和我在ie下显示的顺序不一致
例子:
******
Element   root=doc.createElement( "root ");
root.setAttribute( "c ", "3 ");//先加入的是c
root.setAttribute( "a ", "1 ");//再是a
root.setAttribute( "d ", "4 ");//再是d
root.setAttribute( "b ", "2 ");//最后是b
****

假设生成a.xml文件
前辈们:我预期想得到的是这样的文件
<root   c= '3 ',a= '1 ',d= '4 ',b= '2 '/>
即按照我加入属性的先后顺序显示
然而
ie下显示却是这样
<root   a= '1 ',b= '2 ',c= '3 ',d= '4 '/>
这样的话不是我预期要的东东
希望大家给个解决方案

原创粉丝点击