你可以用xsl做。每个文件写一个xsl。
或者是你用castor。具体用法参考jbuilder里面的帮助,要我介绍,我也是粘贴,还不如自己看,更全面。

解决方案 »

  1.   

    解析xml,然后在重新生成一个
      

  2.   

    to yangtaylor(水无月烈火) ,
    我的XML文件是动态的,怎么能写XSL.
    castor怎么用?.NET直接可以生成.
      

  3.   

    public class TestGetPart
    {
        private static java.util.ArrayList tagLib=new java.util.ArrayList();
        class MyHandler
            extends org.xml.sax.helpers.DefaultHandler
        {
            private String part = null;
            private String startTag,endTag;
            private String getAttr(org.xml.sax.Attributes t)
            {
                String s="";
                for(int i=0;i<t.getLength();i++)
                {
                    s=s+" " +t.getQName(i) +"=\""+ t.getValue(i)+"\"";
                }
                return s;
            }        public String getMatchResult(String qName)
            {
                String result="";
                for(int i=0;i<tagLib.size();i++)
                    if (qName.equals(tagLib.get(i)))
                    {
                        result=qName;
                        break;
                    }
                return result;
            }        public void startElement(String uri, String localName, String qName,
                                     org.xml.sax.Attributes attributes)
            {
                String tag=getMatchResult(qName);
                if ( tag.length()>0 )
                {
                    part = "<" + tag + getAttr(attributes)+ ">";
                }
                else if (part != null)
                {
                    part += "<" + qName + getAttr(attributes) + ">";
                }
            }
            public void endElement(String uri, String localName, String qName)
            {
                String tag=getMatchResult(qName);
                if (tag.length()>0)
                {
                    System.out.println(tag+".xml");
                    part += "</" + tag + ">";
                    System.out.println(part);
                }
                else
                {
                    part += "</" + qName + ">";
                }
            }
            public void characters(char[] ch, int start, int length)
            {
                part += new String(ch, start, length);
            }
        }
        public static void addFilterTag(String tag)
        {
            tagLib.add(tag);
        }    public static java.io.InputStream getXmlStream()
        {
            String xml = "<a><b>ewqewq</b><c id=\"34\"><we we=\"43\"/></c></a>";
            addFilterTag("b");
            addFilterTag("c");
            return new java.io.ByteArrayInputStream(xml.getBytes());
        }
        public static void main(String[] args) throws Exception
        {
            javax.xml.parsers.SAXParserFactory saxParserFtr =
                javax.xml.parsers.SAXParserFactory.newInstance();
            javax.xml.parsers.SAXParser parser = saxParserFtr.newSAXParser();
            java.io.InputStream is = getXmlStream();
            parser.parse(new org.xml.sax.InputSource(is),
                         new TestGetPart().new MyHandler());
        }
    }
      

  4.   

    package jdomtest;import org.jdom.*;
    import org.jdom.output.*;
    import org.jdom.input.*;
    import java.io.*;public class jdomtest{
        public static void main(String args[])throws Exception{        SAXBuilder sb = new SAXBuilder();        //从文件构造一个Document,因为XML文件中已经指定了编码,所以这里不必了
            Document doc = sb.build(new FileInputStream("exampleA.xml"));
            //加入一条处理指令
    //       ProcessingInstruction pi = new ProcessingInstruction
    //            ("xml-stylesheet","href=\"bookList.html.xsl\" type=\"text/xsl\"");
    //        doc.addContent(pi);
            Element root = doc.getRootElement(); //得到根元素
            java.util.List books = root.getChildren(); //得到根元素所有子元素的集合
            Element book = (Element)books.get(0); //得到第一个book元素
            //为第一本书添加一条属性
    //        Attribute a = new Attribute("hot","true");
    //        book.setAttribute(a);
            Element author = book.getChild("author"); //得到指定的字元素
            author.setText("mhj"); //将作者改为mhj
            //或 Text t = new Text("王五");book.addContent(t);
            Element price = book.getChild("price"); //得到指定的字元素        System.out.println(price.getText() );//得到值
            //修改价格,比较郁闷的是我们必须自己转换数据类型,而这正是JAXB的优势
            price.setText(Float.toString(50.0f));//        String indent = "    ";
    //        boolean newLines = true;
            String indent = "";
            boolean newLines = false;
            XMLOutputter outp = new XMLOutputter(indent,newLines,"GBK");
            outp.output(doc, new FileOutputStream("exampleB.xml"));    }
    };<?xml version="1.0" encoding="GBK"?>
    <bookList>
        <book>
            <name>Java编程入门</name>
            <author>张三</author>
            <publishDate>2002-6-6</publishDate>
            <price>35.0</price>
        </book>
        <book>
            <name>XML在Java中的应用</name>
            <author>李四</author>
            <publishDate>2002-9-16</publishDate>
            <price>92.0</price>
        </book>
    </bookList>
    <?xml version="1.0" encoding="GBK"?><bookList>
        <book>
            <name>Java编程入门</name>
            <author>mhj</author>
            <publishDate>2002-6-6</publishDate>
            <price>50.0</price>
        </book>
        <book>
            <name>XML在Java中的应用</name>
            <author>李四</author>
            <publishDate>2002-9-16</publishDate>
            <price>92.0</price>
        </book>
    </bookList>摘自cnjsp..
      

  5.   

    to: kjeny2002(小杭) 
    你的代码我实验过了。
    对楼主的问题没有任何帮助。但,还是非常感谢你。
    因为我发现了JDom的其他使用方法:)。