直接用SAX比较麻烦吧?另外,你合并的条件是什么?

解决方案 »

  1.   

    谢谢沙发,终于有人理我了
    我现在的项目要处理的文件数量比较大,
    考虑到速度问题,只能使用sax
    合并的条件是:
    假如现在有两个xml文件,一个是
    a.xml:
    <a1>aaa
        <a2>1234</a2>
    </a1>
    另一个是
    b.xml:
    <b1>bbb
        <b2>5678</b2>
    </b1>
    我想要实现的是,在用sax解析a.xml时,
    在startElement时,
    把b.xml插入a.xml,把a.xml变成:
    <a1>aaa
        <a2>1234</a2>
        <b1>bbb
            <b2>5678</b2>
        </b1>
    </a1>
    请高手们多多指教,多谢啦
      

  2.   

    没遇到过这类问题,只能给你提供一个思路。使用两个线程分别分析两个XML文件,然后手工输入到一个输入流里。你可以参考下面的代码    public void startElement(String namespaceURI,
                                 String sName, // simple name
                                 String qName, // qualified name
                                 Attributes attrs)
        throws SAXException
        {        // 等待另外一个线程的分析,使按确定的顺序输出
            synchronized(this){
                try {
                    wait();
                } catch (InterruptedException ire) {
                }
            }        echoText();
            String eName = sName; // element name
            if ("".equals(eName)) eName = qName; // not namespaceAware
            emit("<"+eName);
            if (attrs != null) {
                for (int i = 0; i < attrs.getLength(); i++) {
                    String aName = attrs.getLocalName(i); // Attr name 
                    if ("".equals(aName)) aName = attrs.getQName(i);
                    emit(" ");
                    emit(aName+"=\""+attrs.getValue(i)+"\"");
                              }
            }
            emit(">");
        }这段代码来自"J2EE 1.4 Tutorial(http://java.sun.com/j2ee/1.4/download.html#tutorial)"第3章中的例子"echo01.java"