Kavi Chinna
Kavi Chinna

Reputation: 297

Error occurred while parsing XML content from a text file

I am trying to read data from a XML file. I am using dom4j for parsing that xml file. I have tried some code to read that xml file see following my code:

This is my XML file:

<Event xmlns='http://schemas.microsoft.com/win/2004/08/events/event'>
<System>
    <Provider Name='Outlook'/>
    <EventID Qualifiers='16384'>63</EventID>
    <Level>4</Level>
    <Task>0</Task>
    <Keywords>0x80000000000000</Keywords>
    <TimeCreated SystemTime='2015-07-23T13:45:26.000000000Z'/>
    <EventRecordID>27487</EventRecordID>
    <Channel>Application</Channel>
    <Computer>eGLAP0011-PC</Computer>
</System>
<EventData>
    <Data>The Exchange web service request GetAppManifests succeeded. </Data>
</EventData>
</Event>

This is my Java class

import java.util.*;
import java.io.*;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

public class crashJava {


    public static void main(String[] args) {

        try
        {
            File inputFile = new File("D:\\EventLog\\xml_op.xml");
             SAXReader reader = new SAXReader();
             Document document = reader.read( inputFile );
             System.out.println("Root element :"+ document.getRootElement().getName());
             Element classElement = document.getRootElement();
             List<Node> nodes = document.selectNodes("//Event/System");
             System.out.println(nodes);

             for (Node node : nodes) {
                 System.out.println("Node :"+ node.getName());
                 System.out.println("Provider Name : "+ node.valueOf("@Name") );
                 System.out.println("EventID : "+ node.selectSingleNode("EventID").getText());
                 System.out.println("Level : " + node.selectSingleNode("Level").getText());
                 System.out.println("Task : "+ node.selectSingleNode("Task").getText());
                 System.out.println("Keywords : "+ node.selectSingleNode("Keywords").getText());
              }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

    }

}

When I execute the above code it gives the following output to me:

Root element :Event
[]

Please share your ideas

Upvotes: 0

Views: 1769

Answers (3)

forty-two
forty-two

Reputation: 12817

The direct XPath support in DOM4J does not support namespaces, unfortunately. And since the document you are trying to parse includes a namespace for all elements, direct selectNodes() methods will fail.

You need to create an XPath object with a suitable expression and attach a namespace context to it. Something like this:

Map<String, String> nsmap = ...;
nsmap.put("e", "http://schemas.microsoft.com/win/2004/08/events/event");
XPath xp = new DefaultXPath("/e:Event/e:System");
xp.setNamespaceURIs(nsmap);

List<Node> nodes = xp.selectNodes(document);

As an alternative, you could rewrite your XPath expression to use wildcards and a predicate with the local-name function:

/*[local-name() = 'Event']/*[local-name() = 'System']

Upvotes: 0

Tnadev
Tnadev

Reputation: 10072

Add the jaxen jar in your servers lib folder http://jaxen.codehaus.org/releases.html

Remember: jars in the lib/ directory will load automatically

Upvotes: 0

Jens
Jens

Reputation: 69440

You miss the jaxen.jar in your classpath. Download it and add it to the classpath.

Upvotes: 1

Related Questions