com.lx.entity.XmlNode Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lxboot3 Show documentation
Show all versions of lxboot3 Show documentation
使用文档: https://a7fi97h1rc.feishu.cn/docx/X3LRdtLhkoXQ8hxgXDQc2CLOnEg?from=from_copylink
package com.lx.entity;
import com.lx.util.LX;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.StringWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class XmlNode {
private Node node;
public XmlNode(Node node){
this.node = node;
}
//说明: 是节点
/** @author ylx 2022/6/15 0015 22:26 */
private boolean isNode(){
NodeList childNodes = this.node.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
Node item = childNodes.item(i);
if (!"#text".equals(item.getNodeName())){
return true;
}
}
return false;
}
//说明: 获取字符串类型
/** @author ylx 2022/6/15 0015 21:27 */
public String getString(){
if (isNode()){
StringBuilder sb = new StringBuilder();
NodeList childNodes = this.node.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
Node item = childNodes.item(i);
if (!"#text".equals(item.getNodeName())){
sb.append(nodetoString(item));
}
}
return sb.toString();
}else{
return node.getTextContent();
}
}
public int getInt(){
if (isNode()){
throw new ClassCastException("该节点不是int类型:"+nodetoString(node));
}else{
return Integer.parseInt(node.getTextContent());
}
}
public long getLong(){
if (isNode()){
throw new ClassCastException("该节点不是long类型:"+nodetoString(node));
}else{
return Long.parseLong(node.getTextContent());
}
}
public BigDecimal getBigDecimal(){
if (isNode()){
throw new ClassCastException("该节点不是数字类型:"+nodetoString(node));
}else{
return LX.getBigDecimal(node.getTextContent());
}
}
public Map getMap(){
if (!isNode()){
throw new ClassCastException("该节点不是Map类型:"+nodetoString(node));
}else{
Map map = new HashMap<>();
NodeList childNodes = this.node.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
Node item = childNodes.item(i);
if (!"#text".equals(item.getNodeName())){
map.put(item.getNodeName() , new XmlNode(item));
}
}
return map;
}
}
public Map getStringMap(){
if (!isNode()){
throw new ClassCastException("该节点不是Map类型:"+nodetoString(node));
}else{
Map map = new HashMap<>();
NodeList childNodes = this.node.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
Node item = childNodes.item(i);
if (!"#text".equals(item.getNodeName())){
map.put(item.getNodeName() , new XmlNode(item).getString());
}
}
return map;
}
}
public List getList(){
if (!isNode()){
throw new ClassCastException("该节点不是List类型:"+nodetoString(node));
}else{
List map = new ArrayList<>();
NodeList childNodes = this.node.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
Node item = childNodes.item(i);
if (!"#text".equals(item.getNodeName())){
map.add(new XmlNode(item));
}
}
return map;
}
}
public List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy