0

我正在尝试使用 java 创建 xml 树。
我对 JAVA 完全陌生。
我找到了一些代码。

package ep;

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class Tclass {

    public static void main(String argv[]) {

      try {

        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

        // root elements
        Document doc = docBuilder.newDocument();
        Element rootElement = doc.createElement("products");
        doc.appendChild(rootElement);
        for(int x = 1; x < 20; x = x+1) {
        // staff elements
        Element staff = doc.createElement("product_id");
        rootElement.appendChild(staff);

        // set attribute to staff element
        Attr attr = doc.createAttribute("value");

        // shorten way
        staff.setAttribute("value", x);
        }
        // write the content into xml file
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File("file.xml"));

        // Output to console for testing
        // StreamResult result = new StreamResult(System.out);

        transformer.transform(source, result);

        System.out.println("File saved!");

      } catch (ParserConfigurationException pce) {
        pce.printStackTrace();
      } catch (TransformerException tfe) {
        tfe.printStackTrace();
      }
    }
}

它的工作完美。
但我尝试在它们上使用 for-loop 来创建多个元素,然后它在第 40 行返回错误我
The method setAttribute(String, String) in the type Element is not applicable for the arguments (String, int)
有什么问题?
请帮忙。
谢谢...

4

5 回答 5

4

您正在int等待一段时间String

 staff.setAttribute("value", String.valueOf(x));
于 2013-05-27T09:32:21.110 回答
4

当你这样做时:

staff.setAttribute("value", x);

替换为:

staff.setAttribute("value", ""+x);
于 2013-05-27T09:30:12.547 回答
2

Element#setAttribute(name,value),这里这个值是一个简单的string,它在设置时没有被解析。因此,任何标记(例如被识别为实体引用的语法)都被视为文字文本。

所以使用String作为值而不是任何其他类型。因此,将您的int值转换为字符串。

staff.setAttribute("value", Integer.toString(i)); // preferable as static 

或者

staff.setAttribute("value", new Integer(i).toString());

或者

staff.setAttribute("value", ""+i);

或者

staff.setAttribute("value", String.valueOf(i)); // preferable as static
于 2013-05-27T09:32:05.410 回答
1

你应该更换

staff.setAttribute("value", x);

staff.setAttribute("value", String.valueOf(x));
于 2013-05-27T09:31:09.797 回答
1

for()-loop用以下内容替换for(Integer x = 1; x < 20; x = x+1),现在在函数中

staff.setAttribute("value", x.toString());
于 2013-05-27T09:32:31.853 回答