|
1 /* |
|
2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 package com.nokia.s60tools.crashanalyser.model; |
|
19 |
|
20 import org.w3c.dom.*; |
|
21 |
|
22 /** |
|
23 * Utility class for XML handling |
|
24 * |
|
25 */ |
|
26 public final class XmlUtils { |
|
27 private XmlUtils() { |
|
28 // not meant to be initialized |
|
29 } |
|
30 |
|
31 /** |
|
32 * Returns the value of given tagName under given element. |
|
33 * |
|
34 * E.g. |
|
35 * <ele> |
|
36 * <tagName>MyValue</tagName> |
|
37 * </ele> |
|
38 * |
|
39 * MyValue is returned in the above case. |
|
40 * |
|
41 * @param ele xml element under which the tagName should be found |
|
42 * @param tagName tag name of the tag which value is read |
|
43 * @return the value of given tagName under given element. |
|
44 */ |
|
45 public static String getTextValue(Element ele, String tagName) { |
|
46 String textVal = null; |
|
47 |
|
48 try { |
|
49 NodeList nl = ele.getElementsByTagName(tagName); |
|
50 if(nl != null && nl.getLength() > 0) { |
|
51 Element el = (Element)nl.item(0); |
|
52 Node firstChild = el.getFirstChild(); |
|
53 if (firstChild != null) |
|
54 textVal = firstChild.getNodeValue(); |
|
55 } |
|
56 } catch (Exception e) { |
|
57 e.printStackTrace(); |
|
58 } |
|
59 |
|
60 return textVal; |
|
61 } |
|
62 |
|
63 /** |
|
64 * Returns the value of given node |
|
65 * @param node node which value is to be returned |
|
66 * @return the value of given node |
|
67 */ |
|
68 public static String getNodeValue(Node node) { |
|
69 String retval = ""; |
|
70 try { |
|
71 Node firstChild = node.getFirstChild(); |
|
72 if (firstChild != null) |
|
73 retval = firstChild.getNodeValue(); |
|
74 } catch (Exception e) { |
|
75 return ""; |
|
76 } |
|
77 return retval; |
|
78 } |
|
79 |
|
80 /** |
|
81 * Checks whether given xml element contains a node of given name |
|
82 * @param ele xml element |
|
83 * @param tagName name of the node to be found |
|
84 * @return true if node is found under xml element, false if not |
|
85 */ |
|
86 public static boolean containsNode(Element ele, String tagName) { |
|
87 try { |
|
88 NodeList nl = ele.getElementsByTagName(tagName); |
|
89 if(nl != null && nl.getLength() > 0) { |
|
90 return true; |
|
91 } |
|
92 } catch (Exception e) { |
|
93 e.printStackTrace(); |
|
94 } |
|
95 |
|
96 return false; |
|
97 } |
|
98 } |