Compact Class For Java – CCDom


Compact Class is a collection of common functions and classes that can make your code more concise, fast, and precise.

utility

Class CCDom

java.lang.Object

extended by

utility.CCDom
Direct Known Subclasses:
CCDom2nd

public class CCDomextends java.lang.Object

CCDom is a class to make it easier to write to xml.

how to use

 CCDom dom = new CCDom();
 dom.newRoot("root");

 Element element = dom.newElement("elementWithChild");
 dom.setAttribute(element, "attributeName", "attributeValue");
 dom.appendChild_newElement(element, "child");
 dom.appendChild(dom.getRoot(), element);

 element = dom.newElement("elementWithTextNode");
 dom.setAttribute(element, "attributeName", "attributeValue");
 dom.appendChild_newTextNode(element, "textNode");
 dom.appendChild(dom.getRoot(), element);

 dom.transform(new File("fileName.xml"));
Since:
10.03.17
Version:
12.03.17
Author:
Yosua Onesimus


Constructor Detail

CCDom

public CCDom()


Method Detail

newRoot

public org.w3c.dom.Node newRoot(java.lang.String rootTagName)
Create a root with the tag name.

 

Parameters:
rootTagName – The name of the element type to instantiate. For XML,
this is case-sensitive, otherwise it depends on the case-sensitivity of
the markup language in use. In that case, the name is mapped to the
canonical form of that markup by the DOM implementation.

appendChild

public org.w3c.dom.Node appendChild(org.w3c.dom.Element element,
                                    org.w3c.dom.Node newChild)
Adds the node newChild to the end of the list of children of this node.
If the newChild is already in the tree, it is first removed.

 

Parameters:
element
newChild – The node to add.If it is a DocumentFragment object, the
entire contents of the document fragment are moved into the child list of
this node.

 

Returns:The node added.


appendChild_newElement

public org.w3c.dom.Node appendChild_newElement(org.w3c.dom.Element element,
                                               java.lang.String tagName)
Adds the node newChild to the end of the list of children of this node.
If the newChild is already in the tree, it is first removed.

Same as :

 appendChild(element, newElement(tagName));

 

Parameters:
element
tagName – The name of the element type to instantiate. For XML, this
is case-sensitive, otherwise it depends on the case-sensitivity of the
markup language in use. In that case, the name is mapped to the canonical
form of that markup by the DOM implementation.

 

Returns:The node added.


appendChild_newTextNode

public org.w3c.dom.Node appendChild_newTextNode(org.w3c.dom.Element element,
                                                java.lang.String data)
Adds the node newChild to the end of the list of children of this node.
If the newChild is already in the tree, it is first removed.

Same as :

 appendChild(element, getDocument().createTextNode(data));

 

Parameters:
element
data – The data for the node.

Returns:
The node added.

 


newElement

public org.w3c.dom.Element newElement(java.lang.String tagName)
Creates an element of the type specified. Note that the instance returned
implements the Element interface, so attributes can be specified directly
on the returned object.

 

Parameters:
tagName – The name of the element type to instantiate. For XML, this
is case-sensitive, otherwise it depends on the case-sensitivity of the
markup language in use. In that case, the name is mapped to the canonical
form of that markup by the DOM implementation.

 

Returns:A new Element object with the nodeName attribute set to tagName,
and localName, prefix, and namespaceURI set to null.


getDocument

public org.w3c.dom.Document getDocument()
Returns:
document

getRoot

public org.w3c.dom.Element getRoot()
Returns:
root

setAttribute

public void setAttribute(org.w3c.dom.Element element,
                         java.lang.String name,
                         java.lang.String value)
Adds a new attribute. If an attribute with that name is already present
in the element, its value is changed to be that of the value parameter.
This value is a simple string; it is not parsed as it is being set. So
any markup (such as syntax to be recognized as an entity reference) is
treated as literal text, and needs to be appropriately escaped by the
implementation when it is written out. In order to assign an attribute
value that contains entity references, the user must create an Attr node
plus any Text and EntityReference nodes, build the appropriate subtree,
and use setAttributeNode to assign it as the value of an attribute. To
set an attribute with a qualified name and namespace URI, use the
setAttributeNS method.

 

Parameters:
element
name – The name of the attribute to create or alter.
value – Value to set in string form.

 


transform

public boolean transform(java.io.File file)
Transform the XML Source to a Result. Specific transformation behavior is
determined by the settings of the TransformerFactory in effect when the
Transformer was instantiated and any modifications made to the
Transformer instance.

 

Parameters:
file – The Result of transforming the xmlSource.

Returns:
boolean

 


read

public void read(java.io.File file)
Parse the content of the given file as an XML document and return a new
DOM Document object. An IllegalArgumentException is thrown if the File is
null null.

 

Parameters:
file – The file containing the XML to parse.

 


getRootNodeName

public java.lang.String getRootNodeName()
Returns:
The name of this node, depending on its type.

getRootElementsByTagName

public org.w3c.dom.NodeList getRootElementsByTagName(java.lang.String tagName)
Returns a NodeList of all the Elements in document order with a given tag
name and are contained in the document.

 

Parameters:
tagName – The name of the tag to match on. The special value “*”
matches all tags. For XML, the tagname parameter is case-sensitive,
otherwise it depends on the case-sensitivity of the markup language in
use.

 

Returns:A new NodeList object containing all the matched Elements.


getElementsByTagName

public org.w3c.dom.Element getElementsByTagName(org.w3c.dom.Node node,
                                                java.lang.String tagName)
Returns a matching Element with a given tag name, in document order.

 

Parameters:
node
tagName – The name of the tag to match on.

Returns:
A matching Element nodes.

getAttribute

public java.lang.String getAttribute(org.w3c.dom.Node node,
                                     java.lang.String name)
Retrieves an attribute value by name.

 

Parameters:
node
name – The name of the attribute to retrieve.

Returns:
The Attr value as a string, or the empty string if that attribute
does not have a specified or default value.

getAttribute

public java.lang.String getAttribute(org.w3c.dom.Node node,
                                     java.lang.String tagName,
                                     java.lang.String name)
Retrieves an attribute value by name.

 

Parameters:
node
tagName – The name of the tag to match on.
name – The name of the attribute to retrieve.

Returns:
The Attr value as a string, or the empty string if that attribute
does not have a specified or default value.

getNodeValue

public java.lang.String getNodeValue(org.w3c.dom.Node node)
The value of this node, depending on its type; see the table above. When
it is defined to be null, setting it has no effect, including if the node
is read-only.

 

Parameters:
node

Returns:
The value of this node, depending on its type; see the table
above. When it is defined to be null, setting it has no effect, including
if the node is read-only.

 


getNodeValue

public java.lang.String getNodeValue(org.w3c.dom.Node node,
                                     java.lang.String tagName)
The value of this node, depending on its type; see the table above. When
it is defined to be null, setting it has no effect, including if the node
is read-only.

 

Parameters:
node
tagName – The name of the tag to match on.

Returns:
The value of this node, depending on its type; see the table
above. When it is defined to be null, setting it has no effect, including
if the node is read-only.

 

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: