[备忘]使用hutool工具获取xml中元素的属性值,节点值
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);
}
}