0

I am trying to read a xml file from /data/data/aas/a.xml. But I don't know how to get started. I well remounted /data as rw and chmod 777 /data/data/a.xml. It is rooted. It works on adb well .

My xml format is ...

<map>
    <string name="aaa">123</string>
    <string name="bbb">222</string>
    ...
    ...
    <string name="ccc">999</string>
</map>

I want to get the value of ccc(999) in that way. How can I achieve it .Please help me please .. I am very new to android programming ! Please give me full explanation .

Code In my onCreate()..

  process = Runtime.getRuntime().exec("su");
  os = new DataOutputStream(process.getOutputStream());
  os.writeBytes("mount -o remount rw /data");
  os.writeBytes("chmod 777 data/data/com.aas.a/a.xml");

... In a.xml

  <?xml version="1.0" encoding="utf-8" standalone="yes" ?> 
  <map>
     <string name="ccc">0000</string>
  </map>

... I want 0000 from this . Thank you ..

user3517970
  • 63
  • 2
  • 10

3 Answers3

1

Use DOM with java. Here's an example.

antogerva
  • 549
  • 1
  • 9
  • 22
1

You can use dom4j with java and use the XPATH command to get the value you want, if you don't mind to add the library to your project:

public static void main( String[] args ) throws DocumentException{
    org.dom4j.Document doc = null;
    File theXml = new File("pathToXml.xml");

    SAXReader reader = new SAXReader();
    doc = (org.dom4j.Document) reader.read(theXml);
    Element elem = (Element) doc.selectSingleNode("/map/string[@name='ccc']");
    System.out.println(elem.getStringValue());
}
antogerva
  • 549
  • 1
  • 9
  • 22
  • Could you please give me the direct download link jar file to add to my library folder! – user3517970 Jun 15 '14 at 21:07
  • Get [dom4j](http://mvnrepository.com/artifact/dom4j/dom4j/1.6.1) along with [jaxen](http://mvnrepository.com/artifact/jaxen/jaxen/1.1.6) and you should be good to go. – antogerva Jun 15 '14 at 21:16
  • I think you will be annoyed with me .. I am very very new to android programming .. I don't know how to call it on my onCreate() :D .. And this line is under auto correct like that . . I am still dull .. System.out.println(((Node) elem).getStringValue()); – user3517970 Jun 15 '14 at 21:25
  • You should probably create an another question for that. – antogerva Jun 15 '14 at 21:33
  • No problem, I'm Happy to help! That said, if this answer or any other one solved your issue, please mark it as accepted. – antogerva Jun 15 '14 at 21:45
-1

if you want just the ccc of your xml then you can just use a pattern that will split the string between the tag.

try this:

Pattern p = Pattern.compile("name=\"ccc\">(.*?)</string>");
Matcher m = p.matcher(your_xml_here);
if (m.find()) 
  Log.d("result: ",m.group(1)); // => "3"

where your_xml_here is the xml in string form ......

user3517970
  • 63
  • 2
  • 10
Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63