Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old July 30th, 2012, 04:51 PM
neodeveloper neodeveloper is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 1 neodeveloper User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 m 9 sec
Reputation Power: 0
XMLStreamReader

JAVA SE 6
STAX Cursor

I need deperate help in trying to read ann XML file that is structured like below:

bfs_devTest_TodaysDateTime.xml
<?xml version="1.0" encoding="UTF-8"?>
<patient>
<id>7</id>
<name>John Doe</name>
<diagnosis>Recovering</diagnosis>
</patient>
// devTest

The string (GT_devTest_TodaysDateTime.xml) in the header of the doc will change because of the DateTime. I am trying to figure out how to make the Reader/Parser skip the strings at the beginning and end of the XML file. Below is the code I am using.

Thank You Very Much in Advance!!

public class staxXmlReader {

public static void main(String[] args) throws Exception {
String filename = "C:\\Users\\johndoe\\Documents\\DevTest\\XmlStreamR eaderTest.xml";

XMLInputFactory xmlif = null;

xmlif = XMLInputFactory.newInstance();
xmlif.setProperty(XMLInputFactory.IS_REPLACING_ENT ITY_REFERENCES,Boolean.TRUE);
xmlif.setProperty(XMLInputFactory.IS_SUPPORTING_EX TERNAL_ENTITIES,Boolean.FALSE);
xmlif.setProperty(XMLInputFactory.IS_COALESCING, Boolean.FALSE);

try {
XMLStreamReader xmlr = xmlif.createXMLStreamReader(filename,new FileInputStream(filename));
int eventType = xmlr.getEventType();
printStartDocument(xmlr);
while (xmlr.hasNext()) {
eventType = xmlr.next();
printStartElement(xmlr);
printEndElement(xmlr);
printText(xmlr);
printPIData(xmlr);
printComment(xmlr);
}
} catch (XMLStreamException ex) {
System.out.println(ex.getMessage());
if (ex.getNestedException() != null) {
ex.getNestedException()
.printStackTrace();
}
}
}

public static final String getEventTypeString(int eventType) {
switch (eventType) {
case XMLEvent.START_ELEMENT:
return "START_ELEMENT";

case XMLEvent.END_ELEMENT:
return "END_ELEMENT";

case XMLEvent.PROCESSING_INSTRUCTION:
return "PROCESSING_INSTRUCTION";

case XMLEvent.CHARACTERS:
return "CHARACTERS";

case XMLEvent.COMMENT:
return "COMMENT";

case XMLEvent.START_DOCUMENT:
return "START_DOCUMENT";

case XMLEvent.END_DOCUMENT:
return "END_DOCUMENT";

case XMLEvent.ENTITY_REFERENCE:
return "ENTITY_REFERENCE";

case XMLEvent.ATTRIBUTE:
return "ATTRIBUTE";

case XMLEvent.DTD:
return "DTD";

case XMLEvent.CDATA:
return "CDATA";

case XMLEvent.SPACE:
return "SPACE";
}

return "UNKNOWN_EVENT_TYPE , " + eventType;
}

private static void printEventType(int eventType) {
System.out.println("EVENT TYPE(" + eventType + ") = " + getEventTypeString(eventType));
}

private static void printStartDocument(XMLStreamReader xmlr) {
if (xmlr.START_DOCUMENT == xmlr.getEventType()) {
System.out.println(
"<?xml version=\"" + xmlr.getVersion() + "\""
+ " encoding=\"" + xmlr.getCharacterEncodingScheme() + "\""
+ "?>");
}
}

private static void printComment(XMLStreamReader xmlr) {
if (xmlr.getEventType() == xmlr.COMMENT) {
System.out.print("<!--" + xmlr.getText() + "-->");
}
}

private static void printText(XMLStreamReader xmlr) {
if (xmlr.hasText()) {
System.out.print(xmlr.getText());
}
}

private static void printPIData(XMLStreamReader xmlr) {
if (xmlr.getEventType() == XMLEvent.PROCESSING_INSTRUCTION) {
System.out.print(
"<?" + xmlr.getPITarget() + " " + xmlr.getPIData() + "?>");
}
}

private static void printStartElement(XMLStreamReader xmlr) {
if (xmlr.isStartElement()) {
System.out.print("<" + xmlr.getName().toString());
printAttributes(xmlr);
// System.out.print("</codeTitle><cnTitle></cnTitle><codeKeywords></codeKeywords><codeComments></codeComments>");
}
}

private static void printEndElement(XMLStreamReader xmlr) {
if (xmlr.isEndElement()) {
//System.out.print("</" + xmlr.getName().toString() + "</codeTitle><cnTitle></cnTitle><codeKeywords></codeKeywords><codeComments></codeComments>");
System.out.print("</" + xmlr.getName().toString());
}
}

private static void printAttributes(XMLStreamReader xmlr) {
int count = xmlr.getAttributeCount();

if (count > 0) {
for (int i = 0; i < count; i++) {
System.out.print(" ");
System.out.print(xmlr.getAttributeName(i).toString ());
System.out.print("=");
System.out.print("\"");
System.out.print(xmlr.getAttributeValue(i));
System.out.print("\"");
}
}

count = xmlr.getNamespaceCount();

if (count > 0) {
for (int i = 0; i < count; i++) {
System.out.print(" ");
System.out.print("xmlns");

if (xmlr.getNamespacePrefix(i) != null) {
System.out.print(":" + xmlr.getNamespacePrefix(i));
}

System.out.print("=");
System.out.print("\"");
System.out.print(xmlr.getNamespaceURI(i));
System.out.print("\"");
}
}
}
}

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > XMLStreamReader


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap