• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Error while compiling

Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
This is a test java file which parses xml file and return element. When i try to compile it give me error 'class or interface excepted
Document Fragment' .
Do i need to import any other packages. Pls suggest.
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
// Parses a string containing XML and returns a DocumentFragment
// containing the nodes of the parsed XML.
public static DocumentFragment parseXml(Document doc, String fragment) {
// Wrap the fragment in an arbitrary element
fragment = "<fragment>"+fragment+"</fragment>";
try {
// Create a DOM builder and parse the fragment
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
Document d = factory.newDocumentBuilder().parse(
new InputSource(new StringReader(fragment)));

// Import the nodes of the new document into doc so that they
// will be compatible with doc
Node node = doc.importNode(d.getDocumentElement(), true);

// Create the document fragment node to hold the new nodes
DocumentFragment docfrag = doc.createDocumentFragment();

// Move the nodes into the fragment
while (node.hasChildNodes()) {

// Return the fragment
return docfrag;
} catch (SAXException e) {
// A parsing error occurred; the xml input is not valid
} catch (ParserConfigurationException e) {
} catch (IOException e) {
return null;
Posts: 11962
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It might work a bit smoother if you'd put your method inside a class
Always! Wait. Never. Shut up. Look at this tiny ad.
Free, earth friendly heat - from the CodeRanch trailboss
    Bookmark Topic Watch Topic
  • New Topic