2014-10-27 36 views
1
SRC Files 
    - mainPackage 
     IMFL.java 
     performXMLSettings.java 
    - XML 
     settings.xml 

我試圖寫入位於上面軟件包列表中的不同軟件包中的xml文件。 我用寫XML文件中的代碼如下所示:如何寫入不同包中的xml文件

公共類performXMLSettings {

/** 
* 
* @param xml 
* @param root 
* @param equals 
* @return String 
* @throws ParserConfigurationException 
* @throws SAXException 
* @throws IOException 
*/ 
public static String get(String xml, String root, String tag) throws ParserConfigurationException, SAXException, IOException { 
    String ret = ""; 

    InputStream istream = performXMLSettings.class.getClass().getResourceAsStream(xml); 
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 
org.w3c.dom.Document doc = dBuilder.parse(istream); 
    doc.getDocumentElement().normalize(); 
    //ret = doc.getElementsByTagName(tag).item(0).getNodeValue(); 

    NodeList settings = doc.getElementsByTagName(root).item(0).getChildNodes(); 
    for(int j=0;j<settings.getLength();j++) { 
     if(settings.item(j).getNodeName().equals(tag)){ 
      ret = settings.item(j).getTextContent(); 
     } 
    } 
    return ret; 
} 
/** 
* 
* @param xml 
* @param root 
* @param tag 
* @param value 
* @throws ParserConfigurationException 
* @throws SAXException 
* @throws IOException 
* @throws TransformerConfigurationException 
* @throws TransformerException 
* @throws InterruptedException 
*/ 
public static void set(String xml, String root, String tag, String value) throws ParserConfigurationException, SAXException, IOException, TransformerConfigurationException, TransformerException, InterruptedException, URISyntaxException { 

    InputStream istream = performXMLSettings.class.getClass().getResourceAsStream(xml); 
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 
org.w3c.dom.Document doc = dBuilder.parse(istream); 
    doc.getDocumentElement().normalize(); 
    Node rootTag = doc.getElementsByTagName(root).item(0); 
    if(rootTag.hasChildNodes()) { 
     NodeList nl = rootTag.getChildNodes(); 
     for (int i = 0; i < nl.getLength(); i++) { 
      rootTag.removeChild(nl.item(i)); 
     } 
    } 

    Element element = doc.createElement(tag); 
    element.setNodeValue(value); 
    rootTag.appendChild(element); 

    TransformerFactory transformerFactory = TransformerFactory.newInstance(); 
Transformer transformer = transformerFactory.newTransformer(); 
DOMSource source = new DOMSource(doc); 
StreamResult result = new StreamResult(new File(performXMLSettings.class.getResource(xml).toURI())); 
transformer.transform(source, result); 
} 

}

當我試圖從XML文件中的信息,我得到我需要的,但寫作過程不起作用。它不會引發任何錯誤,但在xml文件中沒有新的寫入。

這是我如何使用書寫:

performXMLSettings.set("/XML/settings.xml", "settings", "host", hst);

任何人任何想法,爲什麼?

謝謝!

+0

打印xml變量值以查看您在哪裏設置內容並在此處顯示plz – 2014-10-27 08:25:00

+0

您是指我在哪裏使用設置代碼? – Zoli 2014-10-27 08:44:54

+0

打印變量xml的值以測試您是否在正確的位置寫作? – 2014-10-27 08:50:56

回答

0

看來,我無法回寫到包含該包的jar文件,只有當我打開jar包時,覆蓋xml然後再打包jar包。