3

I have to extract comments from a XML-File. I couldn't find a way to get them using JDOM or something else. At the moment I use Regex and FileReader but I don't feel like this is the way to go.

Can you use something like JDOM to get comments from a XML-File? Or ist it limited to Elements/Nodes and Attributes?

Frank Wittich
  • 139
  • 3
  • 11
  • If you want JDOM way to read comment you can refer http://cafeconleche.org/books/xmljava/chapters/ch15s07.html – AurA Mar 06 '13 at 11:23

3 Answers3

2

I suggest StAX, fastest and simple

    XMLStreamReader xr = XMLInputFactory.newInstance().createXMLStreamReader(
            new FileInputStream("1.xml"));
    while (xr.hasNext()) {
        if (xr.next() == XMLStreamConstants.COMMENT) {
            String comment = xr.getText();
        }
    }
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
1

You need to implement a lexical handler. More information over here - http://docs.oracle.com/javase/tutorial/jaxp/dom/readingXML.html

Look at the Lexical Controls chapter.

user
  • 3,058
  • 23
  • 45
  • Thanks, that helped. Not the lexical control chapter but something about searching nodes, that fits my problem quite good. – Frank Wittich Mar 06 '13 at 12:27
0

Are you using XPath? If yes, you can select comments with comment() function.

jilt3d
  • 3,864
  • 8
  • 34
  • 41