Reputation: 2093
The current problem I'm having is almost exactly the same as a question answered here Convert XML file to CSV. I admit I don't know enough about Java to program anything nearly this complicated myself. I can write scripts that long in a few other languages, but Java has never made any sense to me. However, I can usually adapt other programs well enough. I'm stuck on this one though because when I try to run the code from the accepted answer (without any modifications) I get the following...
>java App
Exception in thread "main" java.lang.NullPointerException
at App$HeaderHandler.addItemHeader(App.java:76)
at App$HeaderHandler.endElement(App.java:99)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:601)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1782)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2939)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:647)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
at App.main(App.java:23)
Now, I know that means I have at least one variable at line 76 is not initialized, it has a null value when it shouldn't. I tried initializing it by placing String headerName = new String("default");
just before it and that prevented it from even compiling:
App.java:76: headerName is already defined in addItemHeader(java.lang.String)
String headerName = new String("default");
I'm not sure where to go from there. Here is the code from lines 75-81
private void addItemHeader(String headerName) {
if (itemHeader.containsKey(headerName)) {
itemHeader.put(headerName, itemHeader.get(headerName) + 1);
} else {
itemHeader.put(headerName, 1);
}
}
If I can get this code, or any like it if someone wants to suggest another method, to work I'm confident I can make small changes and adapt the code.
If nobody can figure out how to fix the NullPointerException I'll also accept any other Java solutions that take XML similar to the following.
<?xml version="1.0" encoding="ISO-10646-UCS-2" standalone="yes" ?>
<ROOT DATE_CREATED="1970-01-01 00:00:01" CREATED_BY="Log Parser">
<ROW>
<EventLog>
Exception
</EventLog>
<RecordNumber>
1381938446
</RecordNumber>
<Message>
An Error Occurred
</Message>
<Data>
</Data>
</ROW>
</ROOT>
And turns it into a flat list by Rows with any sort of delimiter, such as...
Exception|1381938446|An Error Occurred||
Exception|1382348446|Next Error|Some Data Here|
Upvotes: 1
Views: 1136
Reputation: 10553
I think the problem might be here
public void startElement(String uri, String name,
String qName, Attributes atts) {
if ("item".equalsIgnoreCase(qName)) {
itemHeader = new LinkedHashMap<String, Integer>();
}
itemHeader is only initialised when it finds an opening tag. So unless your Xml starts with you will get the current error.
try changing
private LinkedHashMap<String, Integer> itemHeader;
to
private LinkedHashMap<String, Integer> itemHeader = new LinkedHashMap<String, Integer>();
But you will need to carefully look through the code, there will be other changes related changes related to your specific Xml.
Upvotes: 1