Reputation: 107
Here is the XML file snippet that gives some currencies. All of them has values like "currency name", "forex buying", "forex selling" etc...
<?xml version="1.0" encoding="UTF-8"?>
<Currency CrossOrder="1" Kod="AUD" CurrencyCode="AUD">
<Unit>1</Unit>
<Isim>AVUSTRALYA DOLARI</Isim>
<CurrencyName>AUSTRALIAN DOLLAR</CurrencyName>
<ForexBuying>4.4233</ForexBuying>
<ForexSelling>4.4521</ForexSelling>
<BanknoteBuying>4.4030</BanknoteBuying>
<BanknoteSelling>4.4789</BanknoteSelling>
<CrossRateUSD>1.3839</CrossRateUSD>
<CrossRateOther/>
</Currency>
<Currency CrossOrder="2" Kod="DKK" CurrencyCode="DKK">
<Unit>1</Unit>
<Isim>DANIMARKA KRONU</Isim>
<CurrencyName>DANISH KRONE</CurrencyName>
<ForexBuying>0.93070</ForexBuying>
<ForexSelling>0.93527</ForexSelling>
<BanknoteBuying>0.93004</BanknoteBuying>
<BanknoteSelling>0.93742</BanknoteSelling>
<CrossRateUSD>6.5827</CrossRateUSD>
<CrossRateOther/>
</Currency>
And here is my actual code:
import javax.lang.model.element.Element;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.*;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class PasteClass {
public static void main(String[] args) {
try {
File xmlFile = new File("TCMB2.xml");
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
org.w3c.dom.Document document = documentBuilder.parse(xmlFile);
NodeList list = document.getElementsByTagName("Currency");
for (int i = 0; i < list.getLength(); i++) {
Node node = list.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
System.out.println("Kod: "
+ ((org.w3c.dom.Document) element)
.getElementsByTagName("Kod").item(0).getTextContent());
System.out.println("Para Birimi: "
+ ((org.w3c.dom.Document) element)
.getElementsByTagName("Isim").item(0).getTextContent());
System.out.println("Forex Satis Ucreti: "
+ ((org.w3c.dom.Document) element)
.getElementsByTagName("ForexSelling").item(0).getTextContent());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
What I want to do is, simply grabbing some data from an XML file. This is my first work with XML. I just want it to print "kod", "Isim" and "forex Selling" values for each element. But when I run the code, I get this error:
java.lang.ClassCastException: java.xml/com.sun.org.apache.xerces.internal.dom.DeferredElementImpl cannot be cast to java.compiler/javax.lang.model.element.Element at javaPaket.WONTWORK.main(WONTWORK.java:36)
(36th line is the "Element element = (Element) node;" line btw. )
How can I fix that? I copied the similar code from the other site and just changed the values. Yet I got this error.....
Upvotes: 0
Views: 2940
Reputation: 2843
Okay you had a few issues with your code involving imports which people have already brought up, but you had a few other changes that needed to be implemented to your code for it to work the way it looks like you want.
First here is the XML I am using, you did not have a root element which messed up your code, but you did say it wasn't the full file you were using so I was not sure if you needed to see it.
Test.xml:
<root>
<Currency CrossOrder="1" Kod="AUD" CurrencyCode="AUD">
<Unit>1</Unit>
<Isim>AVUSTRALYA DOLARI</Isim>
<CurrencyName>AUSTRALIAN DOLLAR</CurrencyName>
<ForexBuying>4.4233</ForexBuying>
<ForexSelling>4.4521</ForexSelling>
<BanknoteBuying>4.4030</BanknoteBuying>
<BanknoteSelling>4.4789</BanknoteSelling>
<CrossRateUSD>1.3839</CrossRateUSD>
<CrossRateOther/>
</Currency>
<Currency CrossOrder="2" Kod="DKK" CurrencyCode="DKK">
<Unit>1</Unit>
<Isim>DANIMARKA KRONU</Isim>
<CurrencyName>DANISH KRONE</CurrencyName>
<ForexBuying>0.93070</ForexBuying>
<ForexSelling>0.93527</ForexSelling>
<BanknoteBuying>0.93004</BanknoteBuying>
<BanknoteSelling>0.93742</BanknoteSelling>
<CrossRateUSD>6.5827</CrossRateUSD>
<CrossRateOther/>
</Currency>
</root>
And here is the Java that parses your XML:
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.*;
import java.util.HashMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class Test{
public static void main(String[] args) {
/*
These are the values for the xml lines based on your file
Unit = 1
Isim = 2
CurrencyName = 3
ForexBuying = 4
ForexSelling = 5
BanknoteBuying = 6
BanknoteSelling = 7
CrossRateUSD = 8
*/
//create a HashMap to map the loop count to the proper name
//while printing, these are based on numbers commented above
HashMap<Integer, String> mapOfItems = new HashMap<>();
mapOfItems.put(1, "Unit");
mapOfItems.put(2, "Isim");
mapOfItems.put(3, "CurrencyName");
mapOfItems.put(4, "ForexBuying");
mapOfItems.put(5, "ForexSelling");
mapOfItems.put(6, "BanknoteBuying");
mapOfItems.put(7, "BanknoteSelling");
mapOfItems.put(8, "CrossRateUSD");
try {
File xmlFile = new File("test.xml");
DocumentBuilderFactory documentBuilderFactory =
DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder =
documentBuilderFactory.newDocumentBuilder();
org.w3c.dom.Document document =
documentBuilder.parse(xmlFile);
NodeList list = document.getElementsByTagName("Currency");
for(int i=0;i<list.getLength();i++) {
Node node = list.item(i);
if(node.getNodeType() == Node.ELEMENT_NODE) {
//gather node with specific attribute, here item(2) == Kod
String kodValue = node.getAttributes().item(2).getTextContent();
//create array based on text content, split be new line
String[] values = node.getTextContent().split("\n");
System.out.println("\nKOD = "+ kodValue);
for (Integer e = 0; e < values.length; e++) {
//replaceAll() is for regex replacement of white space here
values[e] = values[e].replaceAll(
"^\\s+|\\s+$|\\s*(\n)\\s*|(\\s)\\s*", "$1$2")
.replace("\t"," ");
//just check to make sure null isn't being printed
if(mapOfItems.get(e)!= null)
System.out.println(mapOfItems.get(e)+" = "+values[e]);
}
}
}
}
catch(Exception e) {
e.printStackTrace();
}
}
}
Upvotes: 0
Reputation:
Would you try to run below code? Only the code in the for block was changed.
public static void main(String[] args) {
try {
File xmlFile = new File("TCMB2.xml");
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
org.w3c.dom.Document doc = documentBuilder.parse(xmlFile);
NodeList list = doc.getElementsByTagName("Currency");
for (int i = 0; i < list.getLength(); i++) {
Node node = list.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
System.out.println("Kod: " + element.getAttribute("Kod"));
System.out.println("Para Birimi: " + element.getElementsByTagName("Isim").item(0).getTextContent());
System.out.println("Forex Satis Ucreti: " + element.getElementsByTagName("ForexSelling").item(0).getTextContent()) ;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
Sample Output:
Kod: AUD
Para Birimi: AVUSTRALYA DOLARI
Forex Satis Ucreti: 4.4521
Kod: DKK
Para Birimi: DANIMARKA KRONU
Forex Satis Ucreti: 0.93527
Upvotes: 2
Reputation: 2598
The Issue is that your imports are not correct
import javax.lang.model.element.Element;
is not the same as
import org.w3c.dom.Element;
That is causing conflicts in your java Code. try with this Imports:
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.*;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
Make a simple example and is working.
Try to clean and build it again your code.
Worst case scenarios try with this
org.w3c.dom.Element element = (org.w3c.dom.Element) node;
Upvotes: 1
Reputation: 6123
I think you should do:
Element element = (org.w3c.dom.Element) node;
Because you are also importing javax.lang.model.element.Element
, and I suspect that the casting is occurring with that type (as the stacktrace itself implies).
Upvotes: 0