在Java中解析XML以仅获取文本

时间:2017-05-24 19:34:40

标签: java xml

我有以下XML

<?xml version="1.0" encoding="UTF-8"?>
<wddxPacket version="1.0">
   <header />
   <data>
      <string>
         <char code="0d" />
         <char code="0a" />
         Provider: HERO - 2.xx
         <char code="0d" />
         <char code="0a" />
         <char code="0d" />
         <char code="0a" />
         <char code="0d" />
         <char code="0a" />
         DBvendor=EPA
         <char code="0d" />
         <char code="0a" />
         Text-encoding=UTF-8
         <char code="0d" />
         <char code="0a" />
         <char code="0d" />
         <char code="0a" />
         TY  - RPRT
         <char code="0d" />
         <char code="0a" />
         LB  - 94742
         <char code="0d" />
         <char code="0a" />
         AU  - IARC,
         <char code="0d" />
         <char code="0a" />
         LU  - International Agency for Research on Cancer
         <char code="0d" />
         <char code="0a" />
         PY  - 1985
         <char code="0d" />
         <char code="0a" />
         TY  - JOUR
         <char code="0d" />
         <char code="0a" />
         LB  - 94743
         <char code="0d" />
         <char code="0a" />
         AU  - Shamilov, T. A.
         <char code="0d" />
         <char code="0a" />
         AU  -  Abasov, D. M.
         <char code="0d" />
         <char code="0a" />
         PY  - 1973
         <char code="0d" />
         <char code="0a" />
         J2  - Med Tr Prom Ekol
         <char code="0d" />
         <char code="0a" />
         T2  - Meditsina Truda i Promyshlennaya Ekologiya
         <char code="0d" />
         <char code="0a" />
         JF  - Meditsina Truda i Promyshlennaya Ekologiya
         <char code="0d" />
         <char code="0a" />
         SP  - 12-15
         <char code="0d" />
         <char code="0a" />
         SN  - ISSN 1026-9428
         <char code="0d" />
         <char code="0a" />
         TI  - Effect of allyl chloride on animals under experimental conditions
         <char code="0d" />
         <char code="0a" />
         VL  - 8
         <char code="0d" />
         <char code="0a" />
         ER  -
         <char code="0d" />
         <char code="0a" />
         <char code="0d" />
         <char code="0a" />
         TY  - JOUR
         <char code="0d" />
         <char code="0a" />
      </string>
   </data>
</wddxPacket>

我如何解析它以获得文本?

Provider: HERO - 2.xx
DBvendor=EPA
Text-encoding=UTF-8
TY  - RPRT
LB  - 94742
AU  - IARC,

我需要从TY开始的文本(这是一个RIS格式文件),但如果我能得到所有文本,我仍然可以管理。我在网上尝试但在那里找不到多少。我需要在Java中这样做。

我试过

        Document doc = null;
        DocumentBuilderFactory dbf = null;
        DocumentBuilder docBuild = null;
        dbf = DocumentBuilderFactory.newInstance();
        docBuild = dbf.newDocumentBuilder();
        doc = docBuild.parse(file);
        Node node = doc.getDocumentElement();
        XPathFactory xfact = XPathFactory.newInstance();
        XPath xpath = xfact.newXPath();
        String xpathStr = "/wddxPacket/header/";
        Object res = xpath.evaluate(xpathStr, doc, XPathConstants.NODESET);
        NodeList nodeList = (NodeList) res;

但我一无所获。

3 个答案:

答案 0 :(得分:2)

您需要xpath://string/text()来获取文本值。 以下java代码将为您提供文本值列表。

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

        DocumentBuilder db = dbf.newDocumentBuilder();

        Document doc = db.parse( new File( file ) );

        XPathFactory xPathFactory = XPathFactory.newInstance();
        XPath xpath = xPathFactory.newXPath();

        XPathExpression expr = xpath.compile( "//string/text()");

        Object eval = expr.evaluate( doc, XPathConstants.NODESET );
        List<String> textValues = new ArrayList<String>();
        if ( eval != null && eval instanceof NodeList )
        {
            NodeList list = (NodeList)eval;
            for ( int i = 0 ; i < list.getLength(); i++ )
            {
                Node node = list.item(i);
                String text = node.getNodeValue().trim();

                if ( !text.isEmpty() )
                {
                    System.out.println( text );
                    textValues.add( text );
                }
            }
        }

文本值收集在变量textValues()中。

答案 1 :(得分:1)

你可以用stax

来做
public void getText() {
    String yourSampleFile = "44167076.xml";
    StringBuilder result = new StringBuilder();
    XMLStreamReader r = null;
    try (InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(yourSampleFile)) {
        XMLInputFactory factory = XMLInputFactory.newInstance();
        r = factory.createXMLStreamReader(in);
        while (r.hasNext()) {
            switch (r.getEventType()) {
            case XMLStreamConstants.CHARACTERS:
                result.append(r.getText());
                break;
            default:
                break;
            }
            r.next();
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        if (r != null) {
            try {
                r.close();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

    System.out.println(result.toString().replaceAll("(?m)^[ \t]*\r?\n", ""));
}

打印

     Provider: HERO - 2.xx
     DBvendor=EPA
     Text-encoding=UTF-8
     TY  - RPRT
     LB  - 94742
     AU  - IARC,
     LU  - International Agency for Research on Cancer
     PY  - 1985
     TY  - JOUR
     LB  - 94743
     AU  - Shamilov, T. A.
     AU  -  Abasov, D. M.
     PY  - 1973
     J2  - Med Tr Prom Ekol
     T2  - Meditsina Truda i Promyshlennaya Ekologiya
     JF  - Meditsina Truda i Promyshlennaya Ekologiya
     SP  - 12-15
     SN  - ISSN 1026-9428
     TI  - Effect of allyl chloride on animals under experimental conditions
     VL  - 8
     ER  -
     TY  - JOUR

答案 2 :(得分:1)

two-argument XPath.evaluate method将自动连接任何匹配元素的文本内容。无需显式遍历NodeList。

XPathFactory xfact = XPathFactory.newInstance();
XPath xpath = xfact.newXPath();
String xpathStr = "/wddxPacket/data";

String text;
try (Reader reader = Files.newBufferedReader(Paths.get(filename))) {
    text = xpath.evaluate(xpathStr, new InputSource(reader));
}

for (String line : text.split("\\r?\\n")) {
    line = line.trim();
    if (!line.isEmpty()) {
        System.out.println(line);
    }
}