Aindriu
Aindriu

Reputation: 63

Extracting xml attribute data with Java xstream

I have an cucumber xml report and I want to extract the information in the attributes of the opening xml tag

the XML report looks like this

<testsuite failures="" name="" skipped="" tests="">
  <testcase>
     <skipped>
      more tags but they are not relevant 
     </skipped>
  </testcase>
</testsuite>

I want to be able to take the information from the testsuite attributes save them as java object so that I can put them in an email body that will be sent out to a mailing list.

the information provided in the testsuite xml tag is all I care about.

what would be the best way to approach this? I was creating a Testsuite class, annotating variables for the attributes I want with @XStreamAsAttribute and getting them that way but I am fairly unsure if this is correct.

I have no experience with xstream so trying to go with what information I can find online.

Update - have an issue in the function where I extract the xml attributes, only the last attribute name and value are being returned.

public String extractXMLReportAttributes() {
        String messageBody = "";
    
        for (int i = 0; i < nl.getLength(); i++) {
            Attr a = (Attr) nl.item(i);
            messageBody = String.format("%s: %s", a.getName(), a.getValue());
            System.out.println(messageBody);
        }
        return messageBody;

    }

Upvotes: 0

Views: 158

Answers (2)

Eritrean
Eritrean

Reputation: 16508

If all you need is to grab some attribute values, you may also consider using Jsoup since it is very intuitive and straight forward. Jsoup is a HTML parser but is also capable of parsing XML. Using Jsoup your code could look like something like:

import java.io.File;
import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.parser.Parser;

public class Example {

    public static void main(String[] args) throws IOException {

        final File file = new File("/path/to/your/file.xml");
        Document document = Jsoup.parse(file, "UTF-8", "", Parser.xmlParser());

        Element testsuite = document.selectFirst("testsuite");
        
        String failures   = testsuite.attr("failures");
        String name       = testsuite.attr("name");
        String skipped    = testsuite.attr("skipped");
        String tests      = testsuite.attr("tests");

        System.out.printf("Failures: %s, Name: %s, Skipped: %s, Tests: %s", failures, name, skipped, tests);
    }
}

Add maven dependency or download jar:

<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
<dependency>
    <groupId>org.jsoup</groupId>
    <artifactId>jsoup</artifactId>
    <version>1.16.2</version>
</dependency>

Selector syntax can be found here : Selector

Upvotes: 1

forty-two
forty-two

Reputation: 12817

An example using Java and XPath:

    Document doc = DocumentBuilderFactory.newDefaultInstance()
            .newDocumentBuilder()
            .parse(...);
    XPath xp = XPathFactory.newDefaultInstance().newXPath();
    
    // Get values of known attributes
    System.out.println("Name: " + xp.evaluate("/testsuite/@name", doc));
    System.out.println("Tests: " + xp.evaluate("/testsuite/@tests", doc));
    System.out.println("Failures: " + xp.evaluate("/testsuite/@failures", doc));
    System.out.println("Skipped: " + xp.evaluate("/testsuite/@skipped", doc));

    // Get name and values of arbitrary attributes
    NodeList nl = (NodeList) xp.evaluate("/testsuite/@*", doc, XPathConstants.NODESET);
    for (int i=0; i < nl.getLength(); ++i) {
        Attr a = (Attr) nl.item(i);
        System.out.printf("%s: %s\n", a.getName(), a.getValue());
    }

Upvotes: 0

Related Questions