-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from lunasaw/dev_2.5.3
2.5.3升级部分依赖
- Loading branch information
Showing
3 changed files
with
65 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.luna.common.xml; | ||
|
||
import java.io.StringReader; | ||
import java.io.StringWriter; | ||
|
||
import javax.xml.bind.JAXBContext; | ||
import javax.xml.bind.Marshaller; | ||
import javax.xml.bind.Unmarshaller; | ||
import javax.xml.bind.annotation.XmlAccessType; | ||
import javax.xml.bind.annotation.XmlAccessorType; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.SneakyThrows; | ||
|
||
/** | ||
* @author luna | ||
* @date 2023/10/12 | ||
*/ | ||
@Getter | ||
@Setter | ||
@XmlAccessorType(XmlAccessType.NONE) | ||
public class XmlBean { | ||
|
||
/** | ||
* 字符集, 支持 UTF-8 与 GB2312 | ||
*/ | ||
private String charset = "UTF-8"; | ||
|
||
@SneakyThrows | ||
@Override | ||
public String toString() { | ||
JAXBContext jaxbContext = JAXBContext.newInstance(this.getClass()); | ||
Marshaller marshaller = jaxbContext.createMarshaller(); | ||
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); | ||
marshaller.setProperty(Marshaller.JAXB_ENCODING, charset); | ||
|
||
StringWriter writer = new StringWriter(); | ||
marshaller.marshal(this, writer); | ||
return writer.toString(); | ||
} | ||
|
||
@SneakyThrows | ||
public static <T> Object parseObj(String xmlStr, Class<T> clazz) { | ||
JAXBContext jaxbContext = JAXBContext.newInstance(clazz); | ||
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); | ||
return unmarshaller.unmarshal(new StringReader(xmlStr)); | ||
} | ||
} |