This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<personnes> | |
<personne> | |
<nom>essoussi</nom> | |
<prenom>manel</prenom> | |
<age>25</age> | |
</personne> | |
<personne> | |
<nom>marzouki</nom> | |
<prenom>ramzi</prenom> | |
<age>35</age> | |
</personne> | |
</personnes> |
les jars utilises sont :
json-lib-2.4-jdk15.jar
cayenne-1.2.1 .jar
commons-beanutils-1.3.jar
ezmorph-0.8.1.jar
xom-1.0.jar
pout convertir le contenu xml au format json on fait:
XMLSerializer xmlSerializer = new XMLSerializer();
JSON json= xmlSerializer.read(xml);
xml: est de type chaine des caractères contenant l'xml.
voici le code complet:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.formations.json; | |
import net.sf.json.JSON; | |
import net.sf.json.xml.XMLSerializer; | |
public class Test { | |
public static void main(String[] args) { | |
// TODO Auto-generated method stub | |
String xml="<personnes><personne>" | |
+ "<nom>essoussi</nom>" | |
+"<prenom>manel</prenom>" | |
+"<age>25</age>" | |
+ "</personne>" | |
+ "<personne>" | |
+ "<nom>marzouki</nom>" | |
+"<prenom>ramzi</prenom>" | |
+"<age>35</age>" | |
+ "</personne>" | |
+ "</personnes>"; | |
System.out.println(xml); | |
XMLSerializer xmlSerializer = new XMLSerializer(); | |
JSON json= xmlSerializer.read(xml); | |
System.out.println(json); | |
} | |
} |
Si le xml est dans un fichier on doit faire:
JSON json = xmlSerializer.readFromFile(filexml)
filexml: est le fichier xml existant dans le projet.
voici le vidéo qui montre l'exmeple:
No comments:
Post a Comment