Convert XML String to ArrayList

后端 未结 3 635
悲&欢浪女
悲&欢浪女 2021-01-22 00:04

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

相关标签:
3条回答
  • 2021-01-22 00:17

    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...

    0 讨论(0)
  • 2021-01-22 00:30
      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()); 
      }
    
    0 讨论(0)
  • 2021-01-22 00:41

    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");
    
    0 讨论(0)
提交回复
热议问题