问题如题,描述如下:
…………
Element content1 = document.createElement("person1");
content1.setAttribute(new String("name"), "aaaa");
content1.setAttribute(new String("age"), "1222");
content1.setAttribute(new String("email"), "eejhj");
root.appendChild(content1);
…………
依次添加属性 name 、 age 、email最后输出的属性顺序如下:<person1 age="1222" email="eejhj" name="aaaa" /> Java自动将属性按字母顺序排序了。怎么解决这个问题?注:工作内容需要不自动排序!!!

解决方案 »

  1.   

    package xml;import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;import org.jdom.Attribute;
    import org.jdom.Comment;
    import org.jdom.Document;
    import org.jdom.Element;
    import org.jdom.output.Format;
    import org.jdom.output.XMLOutputter;public class JDomOutput
    {
        public static void main(String[] args) throws IOException
        {
            //创建文档
            Document document = new Document();
            //创建根元素
            Element people = new Element("people");
            //把根元素加入到document中
            document.addContent(people); 
            
            //创建注释
            Comment rootComment = new Comment("将数据从程序输出到XML中!");
            people.addContent(rootComment);
            
            //创建父元素
            Element person1 = new Element("person");
            //把元素加入到根元素中
            people.addContent(person1);
            //设置person1元素属性
            person1.setAttribute("id", "001");
            
            Attribute person1_gender = new Attribute("gender", "male");
            person1.setAttribute(person1_gender);
            
            Attribute person1_aaa = new Attribute("aaa", "bbb");
            person1.setAttribute(person1_aaa);
            
            Element person1_name = new Element("name");
            person1_name.setText("刘德华");
            person1.addContent(person1_name);
            
            Element person1_address = new Element("address");
            person1_address.setText("香港");
            person1.addContent(person1_address);
            
            
            Element person2 = new Element("person");
            people.addContent(person2);
            
            person2.setAttribute("id", "002").setAttribute("gender","male");//添加属性,可以一次添加多个属性
            
            Element person2_name = new Element("name");
            person2_name.setText("林志颖");
            person2.addContent(person2_name);
            
            Element person2_address = new Element("address");
            person2_address.setText("台湾");
            person2.addContent(person2_address);
            
            
            //设置xml输出格式
            Format format = Format.getPrettyFormat();
            format.setEncoding("utf-8");//设置编码
            format.setIndent("    ");//设置缩进
            
            
            //得到xml输出流
            XMLOutputter out = new XMLOutputter(format);
            //把数据输出到xml中
            out.output(document, new FileOutputStream("d:\\jdom.xml"));//或者FileWriter
            
        }}
      

  2.   

    你用的是jdom吧? 换成dom4j 就可以了
      

  3.   

    如果某些原因不能换成dom4j, 那么我告诉你我的做法。。jdom的属性顺序是按照26位英文字母排序的,所以,在你知道你节点个数并且不超过26位的情况下。。你可以这样,,比如你的三个属性name,age,email
    你插入的时候就插入  A__name    B__age    C__email  最后生成DOM之后。。转成String然后用正则表达式  把  [字母__属性名字]  替换成  [属性名字]  前提是你最后的返回结果只需要string。。如果你需要Document类型 就不行了。。
    据我所知。。Dom解析属性和顺序应该没有关系。。除非特殊需求。。是解析定长的String 对属性顺序有要求。。所以我认为你接口返回的应该是个String类型。。如果用不上,全当我废话啊。
      

  4.   

                    String str = “<test A__name='aa' B__age='12' C__email='111'><test>”;
    Pattern pattern = Pattern.compile("\\s\\w{1}__\\w{1,}=");
    Matcher match = pattern.matcher(ismString);
    while(match.find()) {
    String result = match.group();
    str = str.replaceAll(result, " " + result.substring(4, result.length()));
    }