[备忘]使用hutool工具获取xml中元素的属性值,节点值

本方法是基于hutool工具包,org.w3c.dom。与dom4j方法差不多,不过支持XPath了

传入String格式的xml字符串,如果能明确知晓元素节点信息,可以使用XPath表达式获取节点值与元素属性值

import cn.hutool.core.util.XmlUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.xpath.XPathConstants;

/**
 * @author crayon
 * @since 2021/3/30 11:01
 */
public class XmlTest {

    public static void main(String[] args) {
        String xml = "<root><a name = \"第一个元素\"><b>最底层节点值</b></a></root>";
        Document document = XmlUtil.parseXml(xml);
        Element goalElement = XmlUtil.getElementByXPath("//root/a",document);
        Object bString = XmlUtil.getByXPath("//root/a/b", document, XPathConstants.STRING);
        System.out.println("b元素节点值:"+bString);
        String name = goalElement.getAttribute("name");
        System.out.println("a元素属性值:"+name);
    }

}

如果xml节点信息未知,不能使用XPath的xml,可以一个个获取节点元素,判断并获取目标值

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一个简单的备忘录应用程序的代码示例。以下是XMLJava文件的代码: XML文件: ``` <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginTop="16dp" android:layout_marginEnd="16dp" android:hint="Enter your note here" android:inputType="textMultiLine" android:maxLength="500" android:maxLines="10" android:minLines="1" android:textSize="18sp" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/editText" android:layout_alignParentEnd="true" android:layout_marginTop="16dp" android:layout_marginEnd="16dp" android:text="Save" /> <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/button" android:layout_marginTop="16dp" /> </RelativeLayout> ``` Java文件: ``` import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { private EditText editText; private Button button; private ListView listView; private ArrayList<String> notesList; private ArrayAdapter<String> adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = findViewById(R.id.editText); button = findViewById(R.id.button); listView = findViewById(R.id.listView); notesList = new ArrayList<>(); adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, notesList); listView.setAdapter(adapter); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String note = editText.getText().toString(); if (note.isEmpty()) { Toast.makeText(MainActivity.this, "Please enter a note", Toast.LENGTH_SHORT).show(); } else { notesList.add(note); adapter.notifyDataSetChanged(); editText.setText(""); Toast.makeText(MainActivity.this, "Note added successfully", Toast.LENGTH_SHORT).show(); } } }); } } ``` 这是一个简单的备忘录应用程序,用户可以在EditText输入笔记并点击“保存”按钮保存。保存的笔记将显示在ListView。笔记最多可以包含500个字符。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值