2013-05-27 25 views
0

我嘗試在使用java中創建xml樹。
我完全在JAVA中更新鮮。
我找到了一些代碼。
在java中用於循環無效int

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)
什麼問題怎麼辦?
請幫忙。
感謝...

+0

由於這是解決....(Y) –

回答

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 
+0

你可以發佈新的答案獲得'staff.setAttribute(「價值」,隨機數在這裏)的隨機數;' –

+0

好吧,我明白了答案.... –

+0

ummm .......... :) –

4

當你這樣做:

staff.setAttribute("value", x); 

替代者:

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

感謝感謝.... –

+0

歡迎您;) 你可以把它作爲正確的答案:p –

1

就應該更換

staff.setAttribute("value", x); 

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

您正在傳遞一個int,而它的期待String

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

更換for()-loop有以下for(Integer x = 1; x < 20; x = x+1)現在在功能

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