现有A.xml和 B.xml两个文件
其中A.xml格式如下:
<protocol>
<item id="0" name="Protocol" class="com.sensky.mclp.protocol.B" ref="conf/protocol/binary/B.xml" info=""/>
</protocol>B.xml格式如下:
<struct>
<objects>
<object id="0" name="login" info="">
  <param id="0" name="id" type="int" info=""/>
  <param id="1" name="name" type="string" info=""/>
</object>
</objects> <read>
  <param id="0" name="dd" type="int" info=""/>
  <param id="1" name="dd_" type="byte" info=""/>
</read> <write>
  <param id="0" name="ss" type="int" info=""/>
</write>

</struct>
问题是怎么写一个工具类,
把先读一下A.xml文件,根据ref的指向,把B.xml按照一定的格式读到一个.java文件中去
举个例子:
/*info*/
public static final int PROTOCOL = 0;
/*info*/
public static final PROTOCOL_OBJECT_LOGIN = 0;
/*info*/
public static final PROTOCOL_OBJECT_LOGIN_ID = 0;
/*info*/
public static final PROTOCOL_OBJECT_LOGIN_NAME = 1;
/*info*/
public static final PROTOCOL_READ_DD = 0;
/*info*/
public static final PROTOCOL_READ_DD_ = 1;
/*info*/
public static final PROTOCOL_WRITE_SS = 0;急需呀
我还能再加分吗

解决方案 »

  1.   

    不太明白lz的意思,你可以使用现有的xml解析方法来获得B的路径,然后再解析B写道一个java文件中啊。不知道lz是不是这意思?
      

  2.   

    A.xml有B.xml的指向,解析B.xml并且按照格式写入到生成的.java文件中去
      

  3.   

    你可以读取B中的各个节点将其属性值保存下来,然后再依次生成java语句,反正你的java语句都是public static final <属性名称>=<属性值>这种形式的
      

  4.   

    楼主的意思是不是这个
    根据A 的item生成一个类com.sensky.mclp.protocol.B
    <struct>
    <objects>
    <object id="0" name="login" info="">
      <param id="0" name="id" type="int" info=""/>
      <param id="1" name="name" type="string" info=""/>
    </object>
    </objects><read>
      <param id="0" name="dd" type="int" info=""/>
      <param id="1" name="dd_" type="byte" info=""/>
    </read><write>
      <param id="0" name="ss" type="int" info=""/>
    </write></struct> 
    但我不懂 B.xml中,每个节点表示的是什么?
    比如 object, param, id,read... 生成什么呢?属性?那是什么类型?方法?方法体是什么?
    类变量?什么类型?
      

  5.   

    ----------
    这个是要生成的.java文件的格式。比如,读到B.xml文件的要按照这种格式来生成.java文件
      

  6.   

    你的意思好像是要把代码或者是代码的一部分放在xml文件里头,然后再解析生成java文件是吧?如果是这样,提取出xml文件里需要的内容,再生成java文件,不是难事。供参考。
      

  7.   

    也别那么复杂了,怎么实现把B.xml文件读取并且按照
    /*info*/ 
    public static final int PROTOCOL = 0; 
    /*info*/ 
    public static final PROTOCOL_OBJECT_LOGIN = 0; 
    /*info*/ 
    public static final PROTOCOL_OBJECT_LOGIN_ID = 0; 
    /*info*/ 
    public static final PROTOCOL_OBJECT_LOGIN_NAME = 1; 
    /*info*/ 
    public static final PROTOCOL_READ_DD = 0; 
    /*info*/ 
    public static final PROTOCOL_READ_DD_ = 1; 
    /*info*/ 
    这种格式写入一个需要生成的p.java中去(这种格式是和B.xml的有什么属性有关的,可查看B.xml文件)
      

  8.   

    java数据绑定,你查下  casto,或者 jaxb(好像是)
      

  9.   

    看看spring的IOC就知道了,但是你必须知道在你的第一个xml没有加载的时候务必不要让第二个xml加载,如果这样的话可能会出现异常现象的
      

  10.   

    用sax.Jdom等解析A.xml文件.找到节点<item id="0" name="Protocol" class="com.sensky.mclp.protocol.B" ref="conf/protocol/binary/B.xml" info=""/> 
    中的ref信息.把ref信息传给另外一个解析xml文件的方法.就能解析出B.xml文件来.然后用数据绑定或者.....之类的.
      

  11.   

    帮你解析了这两个xml文件,后面自己动脑筋吧。
    /**
     *
     * <p>Title: </p>
     *
     * <p>Description: </p>
     *
     * <p>Copyright: Copyright (c) 2008</p>
     *
     * <p>Company: </p>
     *
     * @author not attributable
     * @version 1.0
     */
    import org.dom4j.io.*;
    import org.dom4j.*;
    import java.io.File;
    import java.util.*;
    public class ToolUtil {    private String bXMLFileName = "";    public ToolUtil(String fileName) {
            bXMLFileName = getRefValue(fileName);
        }
        /**
         * 读取源XML文件
         * @param fileName String 文件名
         * @return Document  返回XML文件
         */
        private Document getDocXML(String fileName) {
            Document doc = null;
            try {
                SAXReader saxReader = new SAXReader();
                doc = saxReader.read(new File(fileName));
            } catch (Exception e) {
                System.out.println(e.getLocalizedMessage());
            }
            return doc;
        }    /**
         * 获取A xml中的ref值
         * @return String
         */
        private String getRefValue(String fileName) {
            Document doc = getDocXML(fileName);
            String ref = doc.getRootElement().element("item").attributeValue("ref");
            return ref;
        }    /**
         * 解析B文档
         */
        public void parseBXML() {
            Document doc = getDocXML(bXMLFileName);
            System.out.println(doc.asXML());        // 循环获取Objects下的子节点
            for (Iterator i = doc.getRootElement().element("objects").
                              elementIterator("object"); i.hasNext(); ) {            Element object = (Element) i.next();
                String id = object.attribute("id").getText();
                String name = object.attribute("name").getText();
                String info = object.attributeValue("info");            // 获取object下的子节点
                for (Iterator j = object.elementIterator("param"); j.hasNext(); ) {
                    Element param = (Element) j.next();
                    String oId = param.attributeValue("id");
                    String oName = param.attributeValue("name");
                    String oType = param.attributeValue("type");
                    String oinfo = param.attributeValue("info");
                }
            }        // 获取read节点
            for (Iterator i = doc.getRootElement().element("read").
                              elementIterator("param"); i.hasNext(); ) {            Element param = (Element) i.next();            String rId = param.attributeValue("id");
                String rName = param.attributeValue("name");
                String rType = param.attributeValue("type");
                String rInfo = param.attributeValue("info");        }        // 获取write 节点
            for (Iterator i = doc.getRootElement().element("read").
                              elementIterator("param"); i.hasNext(); ) {            Element param = (Element) i.next();            String wId = param.attributeValue("id");
                String wName = param.attributeValue("name");
                String wType = param.attributeValue("type");
                String wInfo = param.attributeValue("info");
            }
        }    public static void main(String[] args) {        ToolUtil tool = new ToolUtil("E:\\jbprojects-learn\\NioTest\\A.xml");
            tool.parseBXML();    }}