Convert XML String to ArrayList

后端 未结 3 672
悲&欢浪女
悲&欢浪女 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: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 urls = new ArrayList();
      for (int i = 0; i < nodes.getLength(); i++) {
          urls.add (nodes.item(i).getNodeValue());
          System.out.println(nodes.item(i).getNodeValue()); 
      }
    

提交回复
热议问题