Liu Zhian 2018-01-25 07:20 采纳率: 60%
浏览 1424
已采纳

Android解析XML文件时属性顺序错乱,求指点?

学习Android解析xml文件过程中,发现标签里的属性在被读取后顺序发生错乱。
期待顺序为 :

  <customer name="Andy" tel="888888" email="123456@qq.com">Andy</customer>

读取顺序为:
图片说明

 <!--布局文件如下:-->
 <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.administrator.xmltest.MainActivity">

    <TextView
        android:textSize="25dp"
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="正在读取XML..."
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

<!--自定义看一个customers的xml文件-->
 <?xml version="1.0" encoding="utf-8"?>
<customers>
    <customer name="Andy" tel="888888" email="123456@qq.com">Andy</customer>
    <customer name="Jack" tel="666666" email="654321@qq.com">Jack</customer>
    <customer name="Rose" tel="999999" email="456789@qq.com">Rose</customer>

</customers>
 //   活动代码
 public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        XmlResourceParser xmlResourceParser=getResources().getXml(R.xml.customers);  // 获取到xml文档
        StringBuilder sb=new StringBuilder("");
        try {
            while(xmlResourceParser.getEventType()!=XmlResourceParser.END_DOCUMENT)
            {
                if(xmlResourceParser.getEventType()==XmlResourceParser.START_TAG)   // 开始标记
                {
                    String tag_name=xmlResourceParser.getName();   // 获取到标记名称
                    if(tag_name.equals("customer"))
                    {
                        String name,tel,email;
                        name=xmlResourceParser.getAttributeValue(0);
                        tel=xmlResourceParser.getAttributeValue(1);
                        email=xmlResourceParser.getAttributeValue(2);
                        sb.append("姓名:"+name+" ");
                        sb.append("电话:"+tel+" ");
                        sb.append("邮箱:"+email+" ");
                        sb.append("\n");
                    }
                }
                xmlResourceParser.next();  // 下一个标记
            }
            TextView textView=findViewById(R.id.text);
            textView.setText(sb.toString());
        }catch (Exception e)
        {
            e.printStackTrace();
        }


    }
}
  • 写回答

6条回答 默认 最新

  • qq_19949307 2018-01-25 09:00
    关注

    name=xmlResourceParser.getAttribute('name')
    tel=xmlResourceParser.getAttributeValue('tel');
    email=xmlResourceParser.getAttributeValue('email');

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式