Please join us at the new JavaWorld Q&A Forums. Your existing login will work there. The discussions here are now read-only.


JavaWorld Talkback >> 959305

Pages: 1
JavaWorld
addict


Reged: 06/20/03
Posts: 482
XML DOM-lite parser and writer
      #11341 - 09/05/04 01:10 AM

XML DOM-lite parser and writer

Post Extras: Print Post   Remind Me!   Notify Moderator  
Anonymous
Unregistered




Re: XML DOM-lite parser and writer [Re: JavaWorld]
      #11403 - 09/07/04 09:37 AM

How come the if-statement in serialize is not written as the if-else-statement that it is?

Post Extras: Print Post   Remind Me!   Notify Moderator  
Anonymous
Unregistered




Re: XML DOM-lite parser and writer [Re: Anonymous]
      #11528 - 09/12/04 05:25 PM

possibly because if the first condition is satisfied, the method actually exits from this section.
- use of the else might have some minute amount of overhead? eitherway, the 2nd condition is sometimes never reached to require another if check anyway.


Post Extras: Print Post   Remind Me!   Notify Moderator  
Brad_H_Hansen
stranger


Reged: 09/14/04
Posts: 1
Re: XML DOM-lite parser and writer [Re: Anonymous]
      #11601 - 09/14/04 02:14 PM

Just because we're writing o-o code, that doesn't excuse the use of unstructured techniques. Especially when that sloppy coding style obscures the functioning of the method, as the first commenter observed. The "serialize" method could easily be re-written in a structured (and more clear and consistent) fashion, e.g.:
Code:
 
void serialize(String indent, PrintWriter out) {
out.print(indent + "<" + tag + " " + dumpattributes());

final String closeTag;

if (element == null) {
closeTag = "/>";
}
else if (element.getClass() == Vector.class) {
out.println(">");
for (int i = 0; i < element.size(); i++) {
element.get(i).serialize(indent + " ", out);
}
closeTag = indent + "</" + tag + ">";
}
else {
closeTag = ">" + element + "</" + tag + ">";
}

out.println(closeTag);
return;
}



Post Extras: Print Post   Remind Me!   Notify Moderator  
Anonymous
Unregistered




Re: XML DOM-lite parser and writer [Re: JavaWorld]
      #12014 - 10/01/04 10:37 AM

[image]http://[/image]

Post Extras: Print Post   Remind Me!   Notify Moderator  
Anonymous
Unregistered




Re: XML DOM-lite parser and writer [Re: JavaWorld]
      #12230 - 10/08/04 01:54 AM

This is really great Idea instead using full DOM parser. Please let me know if there are any issues in this.

My id rahul_vsaraf@yahoo.com


Post Extras: Print Post   Remind Me!   Notify Moderator  
Anonymous
Unregistered




Element special characters are not escaped [Re: JavaWorld]
      #12505 - 10/15/04 06:49 AM

The attribute string special characters (e.g. '<') are escaped but not the element string special characters. Is this a mistake or is it a lack of knowledge on my part? Note, the method cleanup() is used in the attribute writing code to escape special characters.

Post Extras: Print Post   Remind Me!   Notify Moderator  
Scott Hiles
Unregistered




Re: Element special characters are not escaped [Re: Anonymous]
      #12903 - 10/27/04 02:36 PM

This was a mistake on my part. I have fixed the issue and tested it and will upload shortly.

Scott


Post Extras: Print Post   Remind Me!   Notify Moderator  
Scott Hiles
Unregistered




Re: XML DOM-lite parser and writer [Re: JavaWorld]
      #12904 - 10/27/04 02:48 PM

I have received some additional methods from Jeff Fern but I haven't had a chance to test them yet. I will send an update to JavaWorld as soon as I have gotten these incorporated.

Scott


Post Extras: Print Post   Remind Me!   Notify Moderator  
Anonymous
Unregistered




Re: XML DOM-lite parser and writer [Re: JavaWorld]
      #18461 - 06/05/05 11:55 AM

s_THOYYETI@yahoo.com

Post Extras: Print Post   Remind Me!   Notify Moderator  
Anonymous
Unregistered




Re: XML DOM-lite parser and writer [Re: Scott Hiles]
      #26276 - 01/24/06 03:12 PM

Have you heard of VTD-XML??
http://vtd-xml.sf.net


Post Extras: Print Post   Remind Me!   Notify Moderator  
ukgremlin
stranger


Reged: 01/30/07
Posts: 1
Re: XML DOM-lite parser and writer [Re: Scott Hiles]
      #43080 - 01/30/07 06:15 AM

I found that the original code has problems with comments in the XML. So I've written some fixes that handle them. The changes follow.

in the main constructor:
Code:

public myXML(BufferedReader in) throws myXMLException, myXMLEncodingException, IOException {
myFileReader reader = new myFileReader(in);
try {
while (this.getTag() == null && reader.ready()) {
// continue until we have a valid root tag or run out of file content
readXML(this, reader);
}
}
catch (RuntimeException e) {
System.err.println("myXML exception "+e);
e.printStackTrace();
throw e;
}
if (debug) System.out.println("XML file successfully loaded");
}



additional method in myFileReader:
Code:

boolean ready() throws IOException {
return reader.ready();
}



the START_TAG case in readXML(myXML xml,myFileReader in):
Code:

case START_TAG:
if (debug) System.out.println("readXML: state START_TAG, c="+(char)ch);
state = popState(stack);
if (ch == '/') {
stack.push(new Integer(state));
state = CLOSE_TAG;
}
else if (ch == '?') {
state = DOCTYPE;
}
else if (xml.getTag() != null) {
in.unread(ch);
in.unread('<');
myXML child = xml.addElement(null);
readXML(child, in);
if (child.getTag() == null) {
xml.removeElement(child);
}
state = popState(stack);
}
else {
stack.push(new Integer(state));
state = OPEN_TAG;
sb.append((char)ch);
}
break;



the COMMENT case in readXML(myXML xml,myFileReader in):
Code:

case COMMENT: // Inside <!-- ... -->
if (debug) System.out.println("readXML: state COMMENT, c="+(char)ch);
if (ch == '>' && sb.toString().endsWith("--")) { // found the end of a comment
sb.setLength(0);
state = popState(stack);
if (debug) System.out.println("readXML: state COMMENT - Comment complete...returning");
return;
}
sb.append((char)ch);
break;



Hope this helps someone make more use of this very handy tool.

Ben


Post Extras: Print Post   Remind Me!   Notify Moderator  
Pages: 1



Extra information
0 registered and 1 anonymous users are browsing this forum.

Moderator:   

Print Topic

Forum Permissions
      You cannot start new topics
      You cannot reply to topics
      HTML is disabled
      UBBCode is enabled

Rating:
Topic views: 8608

Rate this topic

Jump to

Contact us JavaWorld

Powered by UBB.threads™ 6.5.5