gpt4 book ai didi

java - 使用带有 int 值的 setAttribute 方法

转载 作者:行者123 更新时间:2023-11-30 05:25:59 25 4
gpt4 key购买 nike

我试图将 int 值传递到 setAttribute 方法中,但收到错误消息

incompatible types int cannot be converted to string

如何解决这个问题而不弄乱我的代码?

我尝试将字符串解析为 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);
}
}

我希望连接我的 XML 文件,并能够通过我用 javafx 制作的 gui 添加内容

最佳答案

根据Javadoc setAttribute() 方法接受两个参数:属性名称和属性值。两者都是字符串值。现在,这并不意味着 XML 模式将它们视为字符串。该架构可以将您的属性定义为 boolean 值、整数或其他值。但 Java 中的 setter 总是采用字符串。

因此,您需要首先使用 String.valueOf(...) 将 int 值转换为 String。

        watch.setAttribute("ID", String.valueOf(dtoWatch.id));

此外,您可能希望使用 JAXB 而不是直接在 Java 中操作 XML。 JAXB 将为您提供对 XML 结构的良好抽象,并且您将能够轻松地与 XML/Java 进行转换。如果你这样做,那么它会给你一个具有正确数据类型的 set... 方法。看看this tutorial开始吧。

关于java - 使用带有 int 值的 setAttribute 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58599251/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com