• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Error while compiling

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • 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()) {
docfrag.appendChild(node.removeChild(node.getFirstChild()));
}

// 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;
}
regards
Monzy
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It might work a bit smoother if you'd put your method inside a class
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!