2

i have column data is in XML format with Base64 encryption. Now how to read the values

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

and my java code is

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();
}
}

i want read this value value="Q2FsZWmnmewqzRlYXI=" it is possible?

Sri
  • 179
  • 3
  • 9
  • 19
  • 1
    It sounds like what you're *really* asking is "How do I parse XML and retrieve an attribute from it in Java?" The base64 part is simple after that. What have you found so far in your research into XML parsing? – Jon Skeet Apr 05 '16 at 08:52
  • @Jon Skeet.. yes. i'm not started parsing because i have facing this problem first time so i don't know how to start it. – Sri Apr 05 '16 at 08:58
  • Well, if you're new to parsing XML in Java, I'd suggest starting with a tutorial. As it happens, there are lots of ways to parse XML in Java - which is handy in some ways, but not in others. You might want to start with https://docs.oracle.com/javase/tutorial/jaxp/dom/readingXML.html - or maybe have a look at jdom (http://jdom.org/) – Jon Skeet Apr 05 '16 at 09:01

2 Answers2

0

You are getting "xml" as String, which you need to convert in a DOM object to parse it easily.

Here are some of the links which might help you to do what you want,

http://www.journaldev.com/1237/java-convert-string-to-xml-document-and-xml-document-to-string

http://www.coderanch.com/t/512978/java/java/convert-string-xml-file-java

java convert string to xml and parse node

in short,

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();  
DocumentBuilder builder;  

builder = factory.newDocumentBuilder();  
Document doc = builder.parse(new InputSource(new StringReader(xmlStr))); 
return doc;
Community
  • 1
  • 1
darshgohel
  • 382
  • 2
  • 13
0

Use JAXB for xml parsing.

@XmlRootElement(name="event")
@XmlAccessorType(value=XmlAccessType.FIELD)
public class Event
{
    @XmlAttribute(name="id")
    private String id;
    @XmlElement(name="properties")
    private Event.Properties properties;

    @XmlRootElement(name="properties")
    @XmlAccessorType(value=XmlAccessType.FIELD)
    public static class Properties
    {
        @XmlElement(name="property")
        private Event.Property property;
    }

    @XmlRootElement(name="property")
    @XmlAccessorType(value=XmlAccessType.FIELD)
    public static class Property
    {
        @XmlAttribute(name="enc")
        private String enc;
        @XmlAttribute(name="name")
        private String name;
        @XmlAttribute(name="value")
        private String value;
    }
}

Create corresponding getter and setter. Then

JAXBContext context = JAXBContext.newInstance(Event.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
//All the xml data will be mapped into JAXB class instance
Event event = (Event) unmarshaller.unmarshal("xml_file_path");
//get the data
String value = event.getProperties().getProperty().getValue();
String decodedValue = DatatypeConverter.parseBase64Binary(value);
additionster
  • 628
  • 4
  • 14
  • what is the **xml_file_path** in **Event event = (Event) unmarshaller.unmarshal("xml_file_path");** – Sri Apr 11 '16 at 04:10
  • The absolute or relative xml file path. (eg. unmarshaller.unmarshal("/data/event.xml"). – additionster Apr 11 '16 at 07:40
  • **XML** data is in xml object i.e,**String xml = result.getString(1);**.So i can pass **xml** object it is correct? – Sri Apr 12 '16 at 09:30