- All Superinterfaces:
- Node
- See Also:
- Top Examples, Source Code
Node adoptNode(Node source)
throws DOMException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
Attr createAttribute(String name)
throws DOMException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[234]Use the JAXP API to parse an XML document into a DOM tree
By Anonymous on 2005/06/30 12:48:06 Rate
//Here's how you could use the JAXP API to parse an XML document into a DOM tree, and then use the DOM API to extract information from that document.
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
File f; // The file to parse. Assume this is initialized elsewhere
// Create a factory object for creating DOM parsers
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance ( ) ;
// Now use the factory to create a DOM parser ( a.k.a. a DocumentBuilder )
DocumentBuilder parser = factory.newDocumentBuilder ( ) ;
// Parse the file and build a Document tree to represent its content
Document document = parser.parse ( f ) ;
// Ask the document for a list of all < sect1 > tags it contains
NodeList sections = document.getElementsByTagName ( "sect1" ) ;
// Loop through those < sect1 > elements one at a time, and extract the
// content of their < h3 > tags.
int numSections = sections.getLength ( ) ;
for ( int i = 0; i < numSections; i++ ) {
Element section = ( Element ) sections.item ( i ) ; // A < sect1 >
// Find the first element child of this section ( a < h3 > element )
// and print the text it contains.
Node title = section.getFirstChild ( ) ;
while ( title != null && title.getNodeType ( ) != Node.ELEMENT_NODE )
title = title.getNextSibling ( ) ;
// Print the text contained in the Text node child of this element
if ( title!=null ) System.out.println ( title.getFirstChild ( ) .getNodeValue ( ) ) ;
}
//createAttribute
Attr createAttributeNS(String namespaceURI,
String qualifiedName)
throws DOMException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
CDATASection createCDATASection(String data)
throws DOMException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
Comment createComment(String data)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
DocumentFragment createDocumentFragment()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
Element createElement(String tagName)
throws DOMException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
Element createElementNS(String namespaceURI,
String qualifiedName)
throws DOMException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
EntityReference createEntityReference(String name)
throws DOMException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
ProcessingInstruction createProcessingInstruction(String target,
String data)
throws DOMException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[1467]Add processing instructions to the DOM Document before it is marshalled to XML
By Philihp Busby on 2005/06/30 13:07:29 Rate
If my document would be marshalled as
< ?xml version="1.0" encoding="UTF-8" ? >
< x >
< y > aaaaa < /y >
< y > bbbbb < /y >
< z > 123 < /z >
< /x >
I can add processing instructions to the DOM Document before it is marshalled to XML to get
< ?xml version="1.0" encoding="UTF-8" ? >
< ?xml-stylesheet type="text/xsl" HREF="transform.xsl" ? >
< ?xml-stylesheet type="text/css" HREF="display.css" ? >
< x >
< y > aaaaa < /y >
< y > bbbbb < /y >
< z > 123 < /z >
< /x >
with the code
document.insertBefore (
document.createProcessingInstruction (
"xml-stylesheet",
"type=\"text/xsl\" HREF=\"transform.xsl\""
) ,
document.getDocumentElement ( )
) ;
Assuming the browser is smart enough, it will transform the XML using transform.xsl
Text createTextNode(String data)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
DocumentType getDoctype()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[1448]Check to see if DTD is provided for a XML document
By cgselvan2002 { at } yahoo { dot } com (c { dot } gokulaselvan) on 2005/06/06 08:54:14 Rate
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
class doc1
{
public static void main ( String args [ ] )
{
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance ( ) ;
DocumentBuilder parser=factory.newDocumentBuilder ( ) ;
Document docIn=parser.parse ( "C:\\testing xml\\sample.xml" ) ;
DocumentType docType = docIn.getDoctype ( ) ;
if ( docType == null ) {
System.out.println ( "warning: no DTD provided" ) ;
}
}
catch ( Exception ce )
{ System.out.println ( ce.getMessage ( ) ) ; }
}
}
Element getDocumentElement()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[1447]Ask the document for a list of all tags it contains
By cgselvan2002 { at } yahoo { dot } com(c { dot } gokulaselvan) on 2005/06/06 08:23:41 Rate
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
File f=c:\patient.xml; // The file to parse. Assume this is initialized elsewhere
// Create a factory object for creating DOM parsers
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance ( ) ;
// Now use the factory to create a DOM parser ( a.k.a. a DocumentBuilder )
DocumentBuilder parser = factory.newDocumentBuilder ( ) ;
// Parse the file and build a Document tree to represent its content
Document document = parser.parse ( f ) ;
// Ask the document for a list of all < sect1 > tags it contains
Element Ele=getDocumentElement ( ) ;
//it will show all xml convert into String
System.out.println ( Ele.toString ( ) ) ;
String getDocumentURI()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
DOMConfiguration getDomConfig()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
Element getElementById(String elementId)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
NodeList getElementsByTagName(String tagname)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
NodeList getElementsByTagNameNS(String namespaceURI,
String localName)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
DOMImplementation getImplementation()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
String getInputEncoding()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
boolean getStrictErrorChecking()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
String getXmlEncoding()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
boolean getXmlStandalone()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
String getXmlVersion()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
Node importNode(Node importedNode,
boolean deep)
throws DOMException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[1940]importNode(...) example
By lenny { at } cecilrep { dot } com on 2007/11/06 13:27:51 Rate
//example will copy the "Page" node and all its children from the xml file
//at argv [ 0 ] as a child node of the "Top" node of the xml file at
//argv [ 1 ]
public class TestMain {
public static void main ( String [ ] argv ) {
if ( argv.length != 2 ) {
System.err.println ( "Usage: java TransformationApp filename" ) ;
System.exit ( 1 ) ;
}
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance ( ) ;
File ff = new File ( argv [ 1 ] ) ;
DocumentBuilder db = factory.newDocumentBuilder ( ) ;
Document maindocument = db.parse ( ff ) ;
File next = new File ( argv [ 0 ] ) ;
//DocumentBuilderFactory factory2 = DocumentBuilderFactory.newInstance ( ) ;
//DocumentBuilder content1 = factory.newDocumentBuilder ( ) ;
db = factory.newDocumentBuilder ( ) ;
Document nextDocument = db.parse ( next ) ;
NodeList nl = nextDocument.getElementsByTagName ( "Page" ) ;
Node pageNode = nl.item ( 0 ) ;
//import makes the return value it creates importable it doese't actually
//put the importNode ( Node ) arg into the document
Node importableNode = maindocument.importNode ( pageNode, true ) ;
//Where to put the importable node
Node targetNode = ( ( NodeList ) maindocument.getElementsByTagName ( "Top" ) ) .item ( 0 ) ;
targetNode.appendChild ( importableNode ) ; //import it
DOMSource source = new DOMSource ( targetNode ) ; //Convert node to a "source" so it can be made into a file
//Output using an identity transform
StreamResult result = new StreamResult ( System.out ) ;
TransformerFactory tFactory = TransformerFactory.newInstance ( ) ;
Transformer transformer = tFactory.newTransformer ( ) ;
transformer.transform ( source, result ) ;
}
catch ( Exception e )
{
e.printStackTrace ( ) ;
}
void normalizeDocument()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
Node renameNode(Node n,
String namespaceURI,
String qualifiedName)
throws DOMException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[1967]
By Anonymous on 2008/05/20 11:48:14 Rate
how could i repalce all xml element to upercase name by using renamenode
method. ?
void setDocumentURI(String documentURI)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
void setStrictErrorChecking(boolean strictErrorChecking)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
void setXmlStandalone(boolean xmlStandalone)
throws DOMException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
void setXmlVersion(String xmlVersion)
throws DOMException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples