2016-10-26 1 views
0

Je dois créer un fichier XML en utilisant jdom avec un élément imbriqué avec une relation parent-enfant. J'ai écrit le code mais le xml ne correspond pas à mon condition. Dans le commentaire j'ai commenté mon format xml requis. S'il vous plaît aidez-moi ..créer un fichier XML en utilisant jdom dans java

mon code est

public static void main(String[] args) throws IOException { 

    Map<String, String> map = new LinkedHashMap(); 
    map.put("webport", "/"); 
    map.put("webserverobsoluteurl", "https://"); 
    map.put("logger.folder", "/opt/apihandler/logs/wsapi"); 
    map.put("logger.port", "9001"); 
    map.put("logger.allowedlevel", "6"); 
    map.put("logpropsfile", "/opt/apihandler/WSAPIHandler/WEB-INF/log4j.properties"); 
    System.out.println(map); 

    Element root = new Element("worldsmart"); 
    Document doc = new Document(); 
    Element pchild = null; 

    for (Map.Entry<String, String> entry : map.entrySet()) { 
     System.out.println(entry.getKey() + "=====================" + entry.getValue()); 

     if (entry.getKey().contains(".")) { 

      int dotIndex = entry.getKey().indexOf("."); 
      String parentTag = entry.getKey().substring(0, dotIndex); 
      String childTag = entry.getKey().substring(dotIndex + 1, entry.getKey().length()); 
      pchild = new Element(parentTag); 
      pchild.addContent(new Element(childTag).setText(entry.getValue())); 
      root.addContent(pchild); 

     } else { 
      Element child = new Element(entry.getKey()); 
      child.addContent(entry.getValue()); 
      root.addContent(child); 
     } 
    } 
    doc.setRootElement(root); 
    XMLOutputter xmloutput = new XMLOutputter(); 
    xmloutput.setFormat(Format.getPrettyFormat()); 
    xmloutput.output(doc, new FileWriter("/root/Desktop/abc.xml")); 
    System.out.println("file saved"); 
} 

sortie requis:

<worldsmart> 
    <webserverobsoluteurl>https://</webserverobsoluteurl> 
    <webport>/</webport> 
    <logger> 
     <folder>/opt/apihandler/logs/wsapi</folder> 
     <port>9001</port> 
     <allowedlevel>6</allowedlevel> 
    </logger> 
    <logpropsfile> <![CDATA[ /opt/apihandler/WSAPIHandler/WEB-INF/log4j.properties ]]> </logpropsfile> 
</worldsmart> 

mais je reçois un fichier XML comme

<worldsmart> 
    <webport>/</webport> 
    <webserverobsoluteurl>https://</webserverobsoluteurl> 
    <logger> 
     <folder>/opt/apihandler/logs/wsapi</folder> 
    </logger> 
    <logger> 
     <port>9001</port> 
    </logger> 
    <logger> 
     <allowedlevel>6</allowedlevel> 
    </logger> 
    <logpropsfile>/opt/apihandler/WSAPIHandler/WEB-INF/log4j.properties</logpropsfile> 
</worldsmart> 
+0

Vous fractionnez basé sur "" et en ajoutant la balise 'logger' plusieurs fois dans la boucle. Vérifiez s'il existe d'abord avant d'ajouter la balise parent – kjsebastian

+0

vous avez raison conscells mais la balise logger n'existe pas avant d'ajouter la balise parent. Dans mes exigences les valeurs arrivent dynamiquement. – Shubham

Répondre

1

@conscells, dans un commentaire, a la bonne idée de ce que le problème est dans vos boucles - vous créez un nouvel élément logger chaque fois dans la boucle, au lieu de réutiliser le logger s'il existe déjà (et le créer la première fois dans la boucle).

Votre code:

if (entry.getKey().contains(".")) { 

     int dotIndex = entry.getKey().indexOf("."); 
     String parentTag = entry.getKey().substring(0, dotIndex); 
     String childTag = entry.getKey().substring(dotIndex + 1, entry.getKey().length()); 
     pchild = new Element(parentTag); 
     pchild.addContent(new Element(childTag).setText(entry.getValue())); 
     root.addContent(pchild); 

    } 

devrait avoir une condition pour vérifier la pchild première:

if (entry.getKey().contains(".")) { 

     int dotIndex = entry.getKey().indexOf("."); 
     String parentTag = entry.getKey().substring(0, dotIndex); 
     String childTag = entry.getKey().substring(dotIndex + 1, entry.getKey().length()); 

     // locate the previously created `logger` element, if any. 
     pchild = root.getChild(parentTag); 
     if (pchild == null) { 

      // need to add the child if it did not exist. 
      pchild = new Element(parentTag); 
      root.addContent(pchild); 
     } 
     pchild.addContent(new Element(childTag).setText(entry.getValue())); 

    } 
+0

merci rolfl .. (: – Shubham