[英]using the setAttribute method with int value
I am trying to pass a int value into the setAttribute
method but I am getting an error that says我正在尝试将一个 int 值传递给
setAttribute
方法,但我收到一条错误消息
incompatible types int cannot be converted to string
不兼容的类型 int 不能转换为字符串
How do I go about this problem without messing up my code?如何在不弄乱我的代码的情况下解决这个问题?
I have tried to parse the string into a int我试图将字符串解析为 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我希望连接我的 XML 文件,并能够通过我使用 javafx 制作的 gui 向其中添加内容
According to the Javadoc the setAttribute()
method takes in two parameters: the attribute name and the attribute value.根据Javadoc ,
setAttribute()
方法接受两个参数:属性名称和属性值。 Both are string values.两者都是字符串值。 Now, this doesn't mean the XML schema sees them as string.
现在,这并不意味着 XML 架构将它们视为字符串。 The schema could define your attribute as a boolean, integer, or other.
架构可以将您的属性定义为 boolean、integer 或其他。 But the setter in Java will always take a String.
但是 Java 中的设置器将始终采用字符串。
So you'll need to convert your int value into a String first by using String.valueOf(...)
for instance.因此,您需要首先使用
String.valueOf(...)
将您的 int 值转换为 String 。
watch.setAttribute("ID", String.valueOf(dtoWatch.id));
Also, you probably want to use JAXB rather than manipulate XML directly in Java.此外,您可能想要使用 JAXB 而不是直接在 Java 中操作 XML。 JAXB will give you a nice abstraction over the XML structure and you will be able to convert to/from XML/Java easily.
JAXB 将为您提供对 XML 结构的一个很好的抽象,您将能够轻松地转换为 XML/Java。 If you do that, then it will give you a set... method with the right datatype.
如果你这样做,那么它会给你一个 set... 具有正确数据类型的方法。 Have a look at this tutorial to get started.
查看本教程以开始使用。
You could try to parse the int to a string via您可以尝试通过将 int 解析为字符串
String newString = Integer.toString(<int>);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.