com.knowgate.dataxslt
Class XMLDocument

java.lang.Object
  extended by com.knowgate.dataxslt.XMLDocument

public class XMLDocument
extends java.lang.Object

Direct character-level manipulations for XML documents. This class modifies XML document files by directy seeking substrings inside its text. Given a well-knonw XML file structure is easier and faster to seek for <nodes> as substrings instead of parsing the whole documents into a DOM tree.


Constructor Summary
XMLDocument()
           
XMLDocument(java.lang.String sFile)
          Create XMLDocument and load an XML file into memory.
XMLDocument(java.lang.String sFile, java.lang.String sEnc)
          Create XMLDocument and load an XML file into memory.
 
Method Summary
 void addNode(java.lang.String sAfterXPath, java.lang.String sNode)
          Add a piece of XML text after a given node identifier by an XPath expression.
 void addNodeAndSave(java.lang.String sAfterXPath, java.lang.String sNode)
          Add a piece of XML text after a given node and save document.
 java.lang.String getCharacterEncoding()
           
 void load(java.lang.String sFile)
          Load an XML file into memory.
 void load(java.lang.String sFile, java.lang.String sEnc)
          Load an XML file into memory.
 void removeNode(java.lang.String sXPath)
          Remove a node.
 void removeNodeAndSave(java.lang.String sXPath)
          Remove a node and save document.
 void save()
          Save file to disk.
 void save(java.lang.String sFile)
          Save file to disk.
 void setCharacterEncoding(java.lang.String sEnc)
           
 java.lang.String toString()
          Get loaded file as a String
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

XMLDocument

public XMLDocument()

XMLDocument

public XMLDocument(java.lang.String sFile)
            throws java.io.IOException,
                   java.lang.OutOfMemoryError

Create XMLDocument and load an XML file into memory.

No node parsing is done, but file is loaded directly into a String.

Parameters:
sFile - File Path
Throws:
java.io.IOException
java.lang.OutOfMemoryError

XMLDocument

public XMLDocument(java.lang.String sFile,
                   java.lang.String sEnc)
            throws java.io.IOException,
                   java.lang.OutOfMemoryError
Create XMLDocument and load an XML file into memory.

Parameters:
sFile - File Path
sEnc - Character Encoding
Throws:
java.io.IOException
java.lang.OutOfMemoryError
Method Detail

getCharacterEncoding

public java.lang.String getCharacterEncoding()

setCharacterEncoding

public void setCharacterEncoding(java.lang.String sEnc)

load

public void load(java.lang.String sFile,
                 java.lang.String sEnc)
          throws java.io.IOException,
                 java.lang.OutOfMemoryError
Load an XML file into memory. No node parsing is done, but file is loaded directly into a String.

Parameters:
sFile - File Path
sEnc - Character encoding
Throws:
java.io.IOException
java.lang.OutOfMemoryError

load

public void load(java.lang.String sFile)
          throws java.io.IOException,
                 java.lang.OutOfMemoryError

Load an XML file into memory.

No node parsing is done, but file is loaded directly into a String.
Character encoding is UTF-8 by default unless changed by calling setEncoding()

Parameters:
sFile - File Path
Throws:
java.io.IOException
java.lang.OutOfMemoryError

save

public void save(java.lang.String sFile)
          throws java.io.IOException
Save file to disk. If it already exists it is overwritten.

Parameters:
sFile - File Path
Throws:
java.io.IOException

save

public void save()
          throws java.io.IOException
Save file to disk. Save to same path used for loading the file.

Throws:
java.io.IOException

toString

public java.lang.String toString()
Get loaded file as a String

Overrides:
toString in class java.lang.Object

addNode

public void addNode(java.lang.String sAfterXPath,
                    java.lang.String sNode)
             throws org.w3c.dom.DOMException
Add a piece of XML text after a given node identifier by an XPath expression.

Parameters:
sAfterXPath - Restricted XPath expression for node after witch the next node is to be placed. For example :
"pageset/pages/page[@guid="123456789012345678901234567890AB"]/blocks/block[@id="003"]" will add sNode text after ... substring.
"pageset/pages/page[position()=last()]" will add sNode text after last ... substring.
sNode - XML Text to be added.
Throws:
org.w3c.dom.DOMException - DOMException Codes:
NOT_FOUND_ERRA node or attribute from the XPath expression was not found
INVALID_ACCESS_ERRAn attribute expression is invalid
NOT_SUPPORTED_ERRposition() function was used but last() was not specified as value for it

addNodeAndSave

public void addNodeAndSave(java.lang.String sAfterXPath,
                           java.lang.String sNode)
                    throws org.w3c.dom.DOMException,
                           java.io.IOException
Add a piece of XML text after a given node and save document. Document is saved to the same file path where if was loaded.

Parameters:
sAfterXPath - Restricted XPath expression for node after witch the next node is to be placed.
sNode - XML Text to be added.
Throws:
org.w3c.dom.DOMException
java.io.IOException

removeNode

public void removeNode(java.lang.String sXPath)
                throws org.w3c.dom.DOMException
Remove a node.

Parameters:
sXPath - Restricted XPath expression for node to remove. For example: "pageset/pages/page[@guid="123456789012345678901234567890AB"]/blocks/block[@id="003"]" will remove ... substring.
Throws:
org.w3c.dom.DOMException

removeNodeAndSave

public void removeNodeAndSave(java.lang.String sXPath)
                       throws org.w3c.dom.DOMException,
                              java.io.IOException
Remove a node and save document. Document is saved to the same file path where if was loaded.

Parameters:
sXPath - XPath expression for node to remove.
Throws:
org.w3c.dom.DOMException
java.io.IOException