在XML标记之间提取内容

我有这个XML文件:

 findEntitiesResponse   true   0 999999 44   xd 11460 11460 11460 LOGIS COUTTER en Inglés GENERAL GENERAL Default     

有很多CONTENT就像示例中的那个,但我保持简单。

我想要做的是提取标签之间的所有内容 。 我做了很多研究,但我发现最接近的是从一个标签中提取内容。

结果就是这样

  xd 11460 11460 11460 LOGIS COUTTER en Inglés GENERAL GENERAL Default    

记住可能有一个或多个标签。

非常感谢你。

编辑

`public class ReadXMLFile {private final static String filepath =“C:\ Users \ AGOJSO \ Desktop \ jordi \ test.xml”;

 public static void main(String[] args) { printXml(); } public static void printXml() { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try (InputStream in = new FileInputStream(filepath)) { DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(in); NodeList list = filterNodesByXPath(doc, "//root/Entity"); for (int i = 0; i < list.getLength(); i++) { Node node = list.item(i); printNode(node); } } catch (Exception e) { throw new RuntimeException(e); } } private static NodeList filterNodesByXPath(Document doc, String xpathExpr) { try { XPathFactory xPathFactory = XPathFactory.newInstance(); XPath xpath = xPathFactory.newXPath(); XPathExpression expr = xpath.compile(xpathExpr); Object eval = expr.evaluate(doc, XPathConstants.NODESET); return (NodeList) eval; } catch (Exception e) { throw new RuntimeException(e); } } private static void printNode(Node node) throws TransformerFactoryConfigurationError, TransformerException { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(node); transformer.transform(source, result); String xmlString = result.getWriter().toString(); System.out.println(xmlString); } 

}

它不会打印任何错误,因为它似乎什么都不做。

你可以用旧方法做到这一点。

  1. 读取XML到DOM
  2. 使用XPath提取正确的部分
  3. 打印出来……或做任何你喜欢的事情

码:

 @Test public void printXml() { String yourSampleFile = "52720162.xml"; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try (InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(yourSampleFile)) { DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(in); NodeList list = filterNodesByXPath(doc, "//root/Entity"); for (int i = 0; i < list.getLength(); i++) { Node node = list.item(i); printNode(node); } } catch (Exception e) { throw new RuntimeException(e); } } private NodeList filterNodesByXPath(Document doc, String xpathExpr) { try { XPathFactory xPathFactory = XPathFactory.newInstance(); XPath xpath = xPathFactory.newXPath(); XPathExpression expr = xpath.compile(xpathExpr); Object eval = expr.evaluate(doc, XPathConstants.NODESET); return (NodeList) eval; } catch (Exception e) { throw new RuntimeException(e); } } private void printNode(Node node) throws TransformerFactoryConfigurationError, TransformerException { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(node); transformer.transform(source, result); String xmlString = result.getWriter().toString(); System.out.println(xmlString); } 

可以在以下位置找到一个有点概括的forms: 如何使用Java中的XPath读取XML