user1631306
user1631306

Reputation: 4470

Parse XML in Java to get text only

I have following 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>

How can I parse it to get just text?

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

I need the text from TY onwards (which is a RIS format file), but I can still manage if I could get just all text. I tried online but couldn't find much there. I need to do this in Java.

I tried

        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;

but I got nothing.

Upvotes: 0

Views: 823

Answers (3)

VGR
VGR

Reputation: 44404

The two-argument XPath.evaluate method will automatically concatenate the text content of any matched elements. No need to explicitly traverse a 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);
    }
}

Upvotes: 2

jschnasse
jschnasse

Reputation: 9558

You could do it with 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", ""));
}

Prints

     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

Upvotes: 1

SomeDude
SomeDude

Reputation: 14238

You need the xpath : //string/text() to get the text values. The following java code would give you the list of text values.

        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 );
                }
            }
        }

The text values are collected in the variable textValues().

Upvotes: 2

Related Questions