Home arrow static arrow Java Programming [Archive] - Comment Handling in DOM parser
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Comment Handling in DOM parser
This topic has 5 replies on 1 page.

Posts:833
Registered: 1/2/04
Comment Handling in DOM parser  
Jul 2, 2004 7:03 AM



 
I am using a DOM parser. I need to handle xml comments while parsing and building an xml file.
That is, while parsing i need to capture the comments and while building the xml file, i need to insert a customised xml comment node...

Does anybody have any idea how to do this. Any code snippet is higly appreciated.

Thanks
Raheel
 

Posts:580
Registered: 8/21/02
Re: Comment Handling in DOM parser  
Jul 2, 2004 7:37 AM (reply 1 of 5)



 
Here is some code that parses an xml file and adds a comment to the dom tree:[code]
// Parse the file
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// Not really nessesary, as the default value for this property is true, but just to be sure:
factory.setIgnoringComments(false);
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new File("C:/ip3/buildremote.xml"));
// Add a comment just under the root node
doc.getDocumentElement().insertBefore(
doc.createComment("Comment under root element"),
doc.getDocumentElement().getFirstChild());
// Output the file to the console
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer serializer = transFactory.newTransformer();
serializer.transform(new DOMSource(doc), new StreamResult(System.out));
[code]
 

Posts:833
Registered: 1/2/04
Re: Comment Handling in DOM parser  
Jul 2, 2004 7:44 AM (reply 2 of 5)



 
Thanks for your effort, But how do I read it back. I have to create a JTree of the XML Nodes.

For example, if my XML is like this,

<Framework>
<Publisher>
<!--Handler 1 -->
<Handler name="sky"/>
<!--Handler 2 -->
<Handler name="disney"/>
</Publisher>
</Framework>

Then how do I read a comment, which node hold this comment and how do I know it is a commented node or not.

THanks
 

Posts:24,036
Registered: 2/3/03
Re: Comment Handling in DOM parser  
Jul 2, 2004 7:51 AM (reply 3 of 5)



 
It sounds like you may be looking to use the comments for program logic - I'd be careful about doing that, IMHO...
 

Posts:833
Registered: 1/2/04
Re: Comment Handling in DOM parser  
Jul 2, 2004 7:55 AM (reply 4 of 5)



 
It sounds like you may be looking to use the comments
for program logic - I'd be careful about doing that,
IMHO...

My program is basically an XML Editor something like Dreamweaver, and client request for a new feature to handle comments, so that they can write comments in the XML file, I can write the comments without any problem, but next time when you open the XML file, parser ignores the comments and if u save it, comments removed from the XML file.

Thanks
 

Posts:580
Registered: 8/21/02
Re: Comment Handling in DOM parser  
Jul 2, 2004 7:56 AM (reply 5 of 5)



 
The comment node has type of Node.COMMENT_NODE. So, if you want to print all the comments that are children of a node, then you could do something similar to this:
  private void printComments(Node parent) throws DOMException {    NodeList list = parent.getChildNodes();    for (int i = 0; i < list.getLength(); i++) {      Node node = list.item(i);      if (node.getNodeType() == Node.COMMENT_NODE) {        System.out.println("Comment node found. Contents: " +                           node.getNodeValue());      }    }  }
 
This topic has 5 replies on 1 page.