Reputation: 179
i have column data is in XML format with Base64 encryption.i got the values by using java but all values getting i want only particular value
<?xml version="1.0" encoding="UTF-8"?>
<event id="370e7324-3-85ec-63dac16aacb6">
<properties>
<property enc="BASE64" name="state" value="Hrthyw35WmnmewqzRlYXI="/>
<property enc="BASE64" name="record" value="mjhm65WmnmewqzRlYXI="/>
<property enc="BASE64" name="application" value="Q2FsZWmnmewqzRlYXI="/>
</properties>
</event>
and my java code is
try {
Query q="select xml from empdata";
String xml = result.getString(1);
System.out.println("----xml----"+xml);
sqlService.dbRead(connection,sql.toString(),new SqlReader()
{
@Override
public Object readRecord(ResultSet result)
{
try {
String xml = result.getString(1);
// read the xml
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(is);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("property");
String name = null;
String value = null;
for (int temp = 0; temp < nList.getLength(); temp++) {
Node node = nList.item(temp);
Element element=null;
if (node.getNodeType() == Node.ELEMENT_NODE) {
element = (Element) node;
name= element.getAttribute("name");
value = element.getAttribute("value");
System.out.println("--value--"+value+"----name----"+name);
}
}
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
});
}catch (SQLException e) {
e.printStackTrace();
}
i'm getting three names and three values but i want only third name and value.
Upvotes: 3
Views: 6615
Reputation: 1997
Why dont you use JAXB. Most of enterprise applications use JAXB. You could get many tutorials some are below.
As, you have not used JAXB, Lets do it with simplest way without using commandline, maven etc.
Step 1: First you would require to make xsd file. There are many online sites where xsd can be generated. Use http://xmlgrid.net/xml2xsd.html for now. XSD should look like this.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="event">
<xs:complexType>
<xs:sequence>
<xs:element name="properties">
<xs:complexType>
<xs:sequence>
<xs:element name="property" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="enc" type="xs:string"></xs:attribute>
<xs:attribute name="name" type="xs:string"></xs:attribute>
<xs:attribute name="value" type="xs:string"></xs:attribute>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="id" type="xs:string"></xs:attribute>
</xs:complexType>
</xs:element>
</xs:schema>
Edited: You can also make enc,name and value fields optional. If they are not present it will fine also.
Step 2. Now create a simple java project in eclipse and make sure JDK is present in classpath or library not JRE. otherwise you will get below exception.
Error: Could not find or load main class com.sun.tools.internal.xjc.XJCFacade
Now, In eclipse Right Click on Property.xsd -->Generate-->JAXB Classes
NOTE: If changeing it to jdk doesnt work. Make sure you have JAXB required JARs in you classpath.
Step 3. A new source folder will be created with name generated.Now create A class to read xml.
Your PropertyLoad class should look like below.
package generated;
import java.io.File;
import java.util.Iterator;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import generated.Event.Properties.Property;
public class LoadProperties {
public static void main(String[] args) {
try {
File file = new File("src/property.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Event.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Event event = (Event) jaxbUnmarshaller.unmarshal(file);
Iterator<Property> itr = event.getProperties().getProperty().iterator();
while (itr.hasNext()) {
Property prop = itr.next();
System.out.println(
"Encoding : " + prop.getEnc() + "Name : " + prop.getName() + " Value : " + prop.getValue());
}
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
Upvotes: 0
Reputation: 36703
Assuming you want the one that has a name of "application"..
You can also simplify your code, there is no need to check the type of the Nodes, getElementsByTagName will only ever return nodes of type Element.
Example
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<event id=\"370e7324-3-85ec-63dac16aacb6\">\n"
+ "<properties>\n" + "<property enc=\"BASE64\" name=\"state\" value=\"Hrthyw35WmnmewqzRlYXI=\"/>\n"
+ "<property enc=\"BASE64\" name=\"record\" value=\"mjhm65WmnmewqzRlYXI=\"/>\n"
+ "<property enc=\"BASE64\" name=\"application\" value=\"Q2FsZWmnmewqzRlYXI=\"/>\n" + "</properties>\n"
+ "</event>\n";
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(new InputSource(new StringReader(xml)));
doc.getDocumentElement().normalize();
NodeList properties = doc.getElementsByTagName("property");
for (int index = 0; index < properties.getLength(); index++) {
Node node = properties.item(index);
Element element = (Element) node;
if ("application".equals(element.getAttribute("name"))) {
String name = element.getAttribute("name");
String valueEncoded = element.getAttribute("value");
String decoded = new String(Base64.getDecoder().decode(valueEncoded));
System.out.println("--value--" + decoded);
}
}
As an alternative to writing your own filtering logic, you can express this with XPath, a XML selection language with native Java support.
XPath xPath = XPathFactory.newInstance().newXPath();
Element element = (Element) xPath.compile("//property[@name=\"application\"]").evaluate(doc, XPathConstants.NODE);
String value = element.getAttribute("value");
Upvotes: 2
Reputation: 227
If you really need 3rd property (Not "application"), you have to count Nodes..
var nodeIndex = -1;
for (int temp = 0; temp < nList.getLength(); temp++) {
...
if (node.getNodeType() == Node.ELEMENT_NODE) {
nodeIndex++;
...
if (nodeIndex == 2){
...
System.out.println("--value--"+value+"----name----"+name);
}
}
}
Upvotes: 1