在java中使用for循环无效使用int

时间:2013-05-27 09:27:47

标签: java xml

我尝试使用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循环创建多个元素然后它返回我在第40行的错误 The method setAttribute(String, String) in the type Element is not applicable for the arguments (String, int)
我有什么问题?
请帮忙。
感谢...

5 个答案:

答案 0 :(得分:4)

当你这样做时:

staff.setAttribute("value", x);

替换为:

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

答案 1 :(得分:4)

当你期待int时,你正在传递String

 staff.setAttribute("value", String.valueOf(x));

答案 2 :(得分: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

答案 3 :(得分:1)

你应该替换

staff.setAttribute("value", x);

staff.setAttribute("value", String.valueOf(x));

答案 4 :(得分:1)

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

staff.setAttribute("value", x.toString());