Reputation: 57
I am trying to pass a int value into the setAttribute
method but I am getting an error that says
incompatible types int cannot be converted to string
How do I go about this problem without messing up my code?
I have tried to parse the string into a int
public class DAOxml implements DAOInterface {
private Document doc;
@Override
public void addWatch(DTOwatch dtoWatch) {
doc = getDOMDocument();
Element root = doc.getDocumentElement();
Element watch = doc.createElement("watch");
watch.setAttribute("ID", dtoWatch.id);
Element imageText = doc.createElement("imageText");
imageText.setTextContent(dtoWatch.imageText);
Element imageUrl = doc.createElement("imageUrl");
imageUrl.setTextContent(dtoWatch.imageUrl);
Element likes = doc.createElement("likes");
likes.setTextContent(String.valueOf(dtoWatch.likes));
Element dislikes = doc.createElement("dislikes");
dislikes.setTextContent(String.valueOf(dtoWatch.dislikes));
watch.appendChild(imageText);
watch.appendChild(imageUrl);
watch.appendChild(likes);
watch.appendChild(dislikes);
root.appendChild(watch);
writeToXML(doc);
}
@Override
public void deleteWatch(String ID) {
doc = getDOMDocument();
Element root = doc.getDocumentElement();
NodeList nodeList = root.getElementsByTagName("watch");
String sokID;
for (int i = 0; i < nodeList.getLength(); i++) {
Element watch = (Element) nodeList.item(i);
if (watch.hasAttributes()) {
sokID = watch.getAttribute("ID");
if (sokID.equalsIgnoreCase(ID)) {
root.removeChild(watch);
break;
}
}
}
writeToXML(doc);
}
@Override
public void updateWatch(DTOwatch dtoWatch) {
doc = getDOMDocument();
Element root = doc.getDocumentElement();
NodeList nodeList = root.getElementsByTagName("mobil");
String sokID;
for (int i = 0; i < nodeList.getLength(); i++) {
Element watch = (Element) nodeList.item(i);
if (watch.hasAttributes()) {
sokID = watch.getAttribute("ID");
if (sokID.equalsIgnoreCase(dtoWatch.id)) {
watch.getElementsByTagName("imageText").item(0).setTextContent(dtoWatch.imageText);
watch.getElementsByTagName("imageUrl").item(0).setTextContent(dtoWatch.imageUrl);
watch.getElementsByTagName("likes").item(0).setTextContent(String.valueOf(dtoWatch.likes));
watch.getElementsByTagName("dislikes").item(0).setTextContent(String.valueOf(dtoWatch.dislikes));
break;
}
}
}
writeToXML(doc);
}
@Override
public List<DTOwatch> getWatch() {
List<DTOwatch> watch = new ArrayList<>();
doc = getDOMDocument();
Element root = doc.getDocumentElement();
NodeList nodeList = root.getElementsByTagName("watch");
System.out.println(nodeList.getLength());
for (int i = 0; i < nodeList.getLength(); i++) {
Element watch = (Element) nodeList.item(i);
DTOwatch dtoWatch = new DTOwatch();
if (watch.hasAttributes()) {
String ID = watch.getAttribute("ID");
dtoWatch.id = id;
}
dtoWatch.imageText = watch.getElementsByTagName("imageText").item(0).getTextContent();
dtoWatch.imageUrl = watch.getElementsByTagName("imageUrl;").item(0).getTextContent();
dtoWatch.likes = Integer.parseInt(watch.getElementsByTagName("likes").item(0).getTextContent());
dtoWatch.dislikes = Integer.parseInt(watch.getElementsByTagName("dislikes").item(0).getTextContent());
watch.add(dtoWatch);
}
return watch;
}
private Document getDOMDocument() {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(new File("watch.xml"));
} catch (SAXException ex) {
Logger.getLogger(DAOxml.class.getName() + ex.getMessage());
} catch (IOException | ParserConfigurationException ex) {
Logger.getLogger(DAOxml.class.getName() + ex.getMessage());
}
return doc;
}
private void writeToXML(Document doc) {
try {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("watch.xml"));
transformer.transform(source, result);
StreamResult consoleResult = new StreamResult(System.out);
transformer.transform(source, consoleResult);
} catch (TransformerException ex) {
Logger.getLogger(DAOxml.class.getName()).log(Level.SEVERE, null, ex);
}
}
I expect to connect with my XML file and being able to add things to it through my gui that I am making with javafx
Upvotes: 2
Views: 2003
Reputation: 86
You could try to parse the int to a string via
String newString = Integer.toString(<int>);
Upvotes: 0
Reputation: 13832
According to the Javadoc the setAttribute()
method takes in two parameters: the attribute name and the attribute value. Both are string values. Now, this doesn't mean the XML schema sees them as string. The schema could define your attribute as a boolean, integer, or other. But the setter in Java will always take a String.
So you'll need to convert your int value into a String first by using String.valueOf(...)
for instance.
watch.setAttribute("ID", String.valueOf(dtoWatch.id));
Also, you probably want to use JAXB rather than manipulate XML directly in Java. JAXB will give you a nice abstraction over the XML structure and you will be able to convert to/from XML/Java easily. If you do that, then it will give you a set... method with the right datatype. Have a look at this tutorial to get started.
Upvotes: 1