简体   繁体   English

如何读取base64 xml格式的数据以及如何在Java中获取值

[英]How to read base64 xml format data and how to get value in java

In my table i'm maintains one xml column in that column data is in base64 xml format,so now i got this column with query. 在我的表中,我维护一个xml列,因为该列数据为base64 xml格式,所以现在我得到了带有查询的列。

Now how to read xml format data. 现在如何读取xml格式的数据。

here my code: 这是我的代码:

public Object readingSqlResultedRecord(ResultSet result){

try {
Query q="select xml from empdata";
String xml = result.getString(1);
System.out.println("----xml----"+xml);
}catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

and my column is 我的专栏是

<?xml version="1.0" encoding="UTF-8"?>
<event id="370e7324-3-85ec-63dac16aacb6">
<properties>
<property enc="BASE64" name="CHEF:description-html" value="ZmhfrtRo"/>
<property enc="BASE64" name="DAV:name" value="Q2FsZWmnmewqzRlYXI="/>
</properties>
</event>

How to read XML and how to get name value with string format. 如何读取XML以及如何以字符串格式获取名称值。

this code will help you get the id attribute of event. 此代码将帮助您获取事件的id属性。 It will work the same way for other nodes / attributes. 对于其他节点/属性,它将以相同的方式工作。 A good start tutorial : 一个好的开始教程

File fXmlFile = new File("path_to_your_xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
NodeList nList = doc.getElementsByTagName("event");
String id = nList.getAttributes().getNamedItem("id").getNodeValue();

and then the decoding is supported in Java 8 ( java.util.Base64, java.util.Base64.Encoder and java.util.Base64.Decoder ). 然后在Java 8(java.util.Base64,java.util.Base64.Encoder和java.util.Base64.Decoder)中支持解码。 so all you need to do is: 所以您要做的就是:

byte[] decode= Base64.getDecoder().decode(id); 

If I understood your question, your column contains a Base64 value. 如果我理解您的问题,则您的列包含Base64值。 When you do: 当您这样做时:

String xml = result.getString(1);

Variable xml has a Base64 value . 变量xml具有Base64值 So, first you should decode xml variable value.You should use this snippet: 因此,首先您应该解码xml变量值。您应该使用以下代码段:

String decodeXmlValue = BaseEncoding.base64().decode(xml)

If decodeXmlValue contains a XML string, you should unmarshaller the XML value, take a look to Mashaller java documentation . 如果decodeXmlValue包含XML字符串,则应解组XML值,请参阅Mashaller Java文档

Base64 library can be included in your project from maven using this dependency . 使用此依赖关系 ,可以从maven将Base64库包含在您的项目中。 If you are not using maven in your project, you will be able to download the jar from here and include in your project. 如果您不在项目中使用maven,则可以从此处下载jar并将其包含在项目中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM