je cherche a calculer le nombre de <message> pour chaque <element> dans cette document
mais mon code en java il permet de calculer le nombre de message en totale dans le document xml
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 <?xml version="1.0" encoding="UTF-8"?> <root> <data> <element> <application> <id>2305272732</id> <name>Photos</name> </application> <comments> <data> <element> <message>lol kif mté3na mé en noir</message> </element> <element> <message>koll chay bèhi svp donnez nous l,adresse pliz</message> </element> <element> <message>usine rte mahdia km 10 (sal dexpo rte saltnia km 7 )tel 22836915</message> </element> </data> </comments> <created_time>2013-01-27T21:37:04+0000</created_time> <id>203359423100517_407769165970947</id> </element> <element> <created_time>2013-01-31T19:14:30+0000</created_time> <id>203359423100517_303378843098574</id> <type>status</type> </element> <element> <application> <id>2305272732</id> <name>Photos</name> </application> <comments> <data> <element> <message>la table basse est superbe</message> </element> </data> </comments> <created_time>2013-03-20T18:17:16+0000</created_time> <id>203359423100517_147702965397931</id> </element> </data> </root>
merci d'avance
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class fait { public static void main(String[] args) { try { FileInputStream file = new FileInputStream(new File("C:/XML/feed/feed-mars.xml")); DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = builderFactory.newDocumentBuilder(); Document xmlDocument = builder.parse(file); xmlDocument.getDocumentElement().normalize(); XPath xPath = XPathFactory.newInstance().newXPath(); String expression2 = "/root/data/element/comments/data/element"; NodeList nodeList = (NodeList)xPath.compile(expression2).evaluate(xmlDocument,XPathConstants.NODESET); int i=0; int nbc=0; Node node = null; while( (nodeList.getLength()>0)&&(i<nodeList.getLength())) { System.out.println(nodeList.item(i).getFirstChild().getNodeValue()); String idf2= nodeList.item(i).getFirstChild().getNodeValue(); i++; node = (Node) xPath.compile("/root/data/element/comments/data/element/message").evaluate(xmlDocument, XPathConstants.NODE); if (node.getFirstChild().getNodeValue()!="") {nbc++;} } System.out.println("le nb commentaires="+nbc); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace();} } }
Partager