5

Seems like a basic question but I can't find this anywhere. Basically I've got a list of XML links like so: (all in one string)

I already have the "string" var which contains all the XML. Just extracting the HTML strings.

<?xml version="1.0" encoding="UTF-8"?>
<fql_query_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" list="true">
<photo>
    <src_small>http://photos-a.ak.fbcdn.net/hphotos-ak-ash4/486603_10151153207000351_1200565882_t.jpg</src_small>
</photo>
<photo>
  <src_small>http://photos-c.ak.fbcdn.net/hphotos-ak-ash3/578919_10150988289678715_1110488833_t.jpg</src_small>
</photo>

I want to convert these into a arrayList, so something like URLArray[0] would be the first address as a string.

Can anyone tell me how to do this thanks?

4
  • 2
    use an XMLReader to parse the XML, building out your ArrayList during parsing. Commented Aug 6, 2012 at 17:32
  • This question stackoverflow.com/questions/3625506/… has some good XML parser choices for android. Commented Aug 6, 2012 at 17:34
  • Way to much code for something that should be a simple task? Commented Aug 6, 2012 at 17:56
  • 1
    This is one of the best answers for this question stackoverflow.com/a/4828765/1087653 Commented Aug 6, 2012 at 17:57

3 Answers 3

7
  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  DocumentBuilder builder = factory.newDocumentBuilder();
  InputSource is = new InputSource( new StringReader( xmlString) );
  Document doc = builder.parse( is );

  XPathFactory factory = XPathFactory.newInstance();
  XPath xpath = factory.newXPath();
  xpath.setNamespaceContext(new PersonalNamespaceContext());
  XPathExpression expr = xpath.compile("//src_small/text()");

  Object result = expr.evaluate(doc, XPathConstants.NODESET);
  NodeList nodes = (NodeList) result;
  List<String> urls = new ArrayList<String>();
  for (int i = 0; i < nodes.getLength(); i++) {
      urls.add (nodes.item(i).getNodeValue());
      System.out.println(nodes.item(i).getNodeValue()); 
  }
Sign up to request clarification or add additional context in comments.

3 Comments

What's the personal namespace? I'm getting the error PersonalNamespaceContext cannot be resolved to a type
that's the xmlns attribute, remove it and try?
Ah it wasn't in my library, I downloaded here: code.google.com/p/fabulous/source/browse/branches/3.0/3.0.0/src/… Works great, thanks so much you've saved me hours.
3

You are right, there should be some other resources out there that can help you. Maybe your searches just do not use the right keywords.

You basically have 2 choices:

  1. Use an XML processing library. SAX, DOM, XPATH, & xmlreader are some keywords you can use to find some.

  2. Just ignore the fact that your string is xml and perform normal string operations on it. splits, iterate through it, regular expressions, ect...

Comments

-2

Yes for that you have to perform XML Parsing.

then store that in ArrayList.

ex:

ArrayList<String> aList = new ArrayList<String>();

aList.add("your string");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.