2014-01-20 198 views
3

嗨,即時通訊尋找一種解決方案,將Java節點追加到現有的XML文件。 我得到的是這樣的使用java將節點追加到現有的XML文件

<data> 
<people> 
    <person> 
     <firstName>Frank</firstName> 
     <lastName>Erb</lastName> 
     <access>true</access> 
     <images> 
      <img>hm001.jpg</img> 
     </images> 
    </person> 
    <person> 
     <firstName>Hans</firstName> 
     <lastName>Mustermann</lastName> 
     <access>true</access> 
     <images> 
      <img>hm001.jpg</img> 
     </images> 
    </person> 
    <person> 
     <firstName>Thomas</firstName> 
     <lastName>Tester</lastName> 
     <access>false</access> 
     <images> 
      <img>tt001.jpg</img> 
     </images> 
    </person> 
</people> 
</data> 

一個XML文件我whant補充的是與它的人民元素中的元素一個人節點。我的大問題是作爲根節點的數據節點。如果它將作爲根節點,我可以解決它。但我無法設法讓人節點下的人節點。

  <person> 
     <firstName>Tom</firstName> 
     <lastName>Hanks</lastName> 
     <access>false</access> 
     <images> 
      <img>tt001.jpg</img> 
     </images> 
    </person> 

感謝您的幫助!

我的Java代碼看起來就這樣

Element root = document.getDocumentElement(); 


// Root Element 
Element rootElement = document.getDocumentElement(); 

Collection<Server> svr = new ArrayList<Server>(); 
svr.add(new Server()); 

for (Server i : svr) { 
    // server elements 

    Element server = document.createElement("people"); 
    rootElement.appendChild(server); 
    //rootElement.appendChild(server); 

    Element name = document.createElement("person"); 
    server.appendChild(name); 

    Element firstName = document.createElement("firstName"); 
    firstName.appendChild(document.createTextNode(i.getFirstName())); 
    server.appendChild(firstName); 
    name.appendChild(firstName); 

    Element port = document.createElement("lastName"); 
    port.appendChild(document.createTextNode(i.getLastName())); 
    server.appendChild(port); 
    name.appendChild(port); 

    Element access = document.createElement("access"); 
    access.appendChild(document.createTextNode(i.getAccess())); 
    server.appendChild(access); 
    name.appendChild(access); 

    String imageName = Main.randomImgNr+""; 
    Element images = document.createElement("images"); 
    //images.appendChild(document.createTextNode(i.getAccess())); 
    Element img = document.createElement("img"); 
    img.appendChild(document.createTextNode(imageName));//i.getImage())); 
    images.appendChild(img);    

    server.appendChild(images); 
    name.appendChild(images); 
    root.appendChild(server); 
+1

試過使用JAXB?它非常整齊。 –

回答

6

沒有一個庫,你可以做這樣的事情:

Element dataTag = doc.getDocumentElement(); 
Element peopleTag = (Element) dataTag.getElementsByTagName("people").item(0); 

Element newPerson = doc.createElement("person"); 

Element firstName = doc.createElement("firstName"); 
firstName.setTextContent("Tom"); 

Element lastName = doc.createElement("lastName"); 
lastName.setTextContent("Hanks"); 

newPerson.appendChild(firstName); 
newPerson.appendChild(lastName); 

peopleTag.appendChild(newPerson); 

其中成員:

... 
     <person> 
      <firstName>Thomas</firstName> 
      <lastName>Tester</lastName> 
      <access>false</access> 
      <images> 
       <img>tt001.jpg</img> 
      </images> 
     </person> 
     <person> 
      <firstName>Tom</firstName> 
      <lastName>Hanks</lastName> 
     </person> 
    </people> 
</data> 
+0

完美!!!!!這是我正在尋找的。謝謝!! – user3216026

+0

不客氣! –

+0

有沒有可能讓新節點進入正確的格式?每當我嘗試添加這些節點時,它們都卡在左側。我在這裏先向您的幫助表示感謝。 –

1

這是很容易與JOOX庫,例子:

// Parse the document from a file 
Document document = $(xmlFile).document(); 

// Find the order at index 4 and add an element "paid" 
$(document).find("people").children().eq(4).append("<paid>true</paid>"); 

// Find those orders that are paid and flag them as "settled" 
$(document).find("people").children().find("paid").after("<settled>true</settled>"); 
+0

如果不使用庫或包裝器,不可能這樣做嗎? – user3216026

+0

使用庫更容易。 – MariuszS

0

關注這個一般方法:

public static void main(String[] args) { 
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
    DocumentBuilder db = null; 
    try { 
     db = dbf.newDocumentBuilder(); 
     Document doc = db.parse("input.xml"); 
     NodeList people = doc.getElementsByTagName("people"); 
     people.item(0) 
       .appendChild(
         createPersonElement(doc, "Tom", "Hanks", true, 
           "tt001.jpg")); 
     System.out.println(nodeToString(doc)); 
    } catch (SAXException e) { 
     // handle SAXException 
    } catch (IOException e) { 
     // handle IOException 
    } catch (TransformerException e) { 
     // handle TransformerException 
    } catch (ParserConfigurationException e1) { 
     // handle ParserConfigurationException 
    } 
} 

private static Element createPersonElement(Document doc, String firstName, 
     String lastName, Boolean access, String image) { 
    Element el = doc.createElement("person"); 
    el.appendChild(createPersonDetailElement(doc, "firstName", firstName)); 
    el.appendChild(createPersonDetailElement(doc, "lastName", lastName)); 
    el.appendChild(createPersonDetailElement(doc, "access", 
      access.toString())); 
    Element images = doc.createElement("images"); 
    images.appendChild(createPersonDetailElement(doc, "img", image)); 
    el.appendChild(images); 
    return el; 
} 

private static Element createPersonDetailElement(Document doc, String name, 
     String value) { 
    Element el = doc.createElement(name); 
    el.appendChild(doc.createTextNode(value)); 
    return el; 
} 

這使用以下輔助方法來打印結果:

private static String nodeToString(Node node) throws TransformerException { 
    StringWriter buf = new StringWriter(); 
    Transformer xform = TransformerFactory.newInstance().newTransformer(); 
    xform.transform(new DOMSource(node), new StreamResult(buf)); 
    return buf.toString(); 
} 

這可以被修改,以更新原始文件來代替。

-1
public static void main(String[] args) throws ParserConfigurationException, IOException { 
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
    DocumentBuilder db; 
    db = null; 
    try { 
     db = dbf.newDocumentBuilder(); 
     Document doc = db.parse("input.xml"); 
     NodeList people = doc.getElementsByTagName("people"); 
     people.item(0).appendChild(createPersonElement(doc, "Tom", "Hanks", true, "tt001.jpg")); 
     Transformer transformer = TransformerFactory.newInstance().newTransformer(); 
     transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 
     DOMSource source = new DOMSource(doc); 
     StreamResult console = new StreamResult(System.out);//for Console print 
     transformer.transform(source, console); 
     StreamResult file = new StreamResult(new File("input.xml")); 
     transformer.transform(source, file); 
    } catch (SAXException | IOException | TransformerException | ParserConfigurationException e) { 
     System.out.println(e); 
    } 
} 

private static Element createPersonElement(Document doc, String firstName, 
     String lastName, Boolean access, String image) { 
    Element el = doc.createElement("person"); 
    el.appendChild(createPersonDetailElement(doc, "firstName", firstName)); 
    el.appendChild(createPersonDetailElement(doc, "lastName", lastName)); 
    el.appendChild(createPersonDetailElement(doc, "access", 
      access.toString())); 
    Element images = doc.createElement("images"); 
    images.appendChild(createPersonDetailElement(doc, "img", image)); 
    el.appendChild(images); 
    return el; 
} 

private static Element createPersonDetailElement(Document doc, String name, 
     String value) { 
    Element el = doc.createElement(name); 
    el.appendChild(doc.createTextNode(value)); 
    return el; 
} 
相關問題