Thursday, August 16, 2012
Jdom CRUD Operations
Hi,
Jdom CRUD Operations
Here is sample code to do some operations like
//Adding elements
public void addElementAtXpath() {
try {
XPath xpath = XPath.newInstance("publishers");
xpath.addNamespace(root_.getNamespace());
Element publisherNode = (Element) xpath.selectSingleNode(root_);
org.jdom.Element element = new Element("kalees");
element.addContent(createElement(CI_FILE_RELEASE_OVERRIDE_AUTH_NODE, TRUE));
publisherNode.addContent(element);
} catch (Exception e) {
e.printStackTrace();
}
}
// Changing attributes
public void changeAttributeValue() {
try {
XPath xpath = XPath.newInstance("scm");
xpath.addNamespace(root_.getNamespace());
Element scmNode = (Element) xpath.selectSingleNode(root_);
scmNode.setAttribute("class", "Kalees");
} catch (Exception e) {
e.printStackTrace();
}
}
//Deleting Elements
public void deleteElementAtXpath() {
//deleting a node...
// deleting content of a node
//triggerNode.removeContent();
try {
String childName = "hudson.plugins.emailext.ExtendedEmailPublisher";
XPath xpath = XPath.newInstance("publishers");
xpath.addNamespace(root_.getNamespace());
Element triggerNode = (Element) xpath.selectSingleNode(root_); // if it is null then element is not present
triggerNode.removeChild(childName);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Element createElement(String nodeName, String NodeValue) {
org.jdom.Element element = new Element(nodeName);
if (NodeValue != null) {
element.addContent(NodeValue);
}
return element;
}
--
Thank you.
Regards,
Kaleeswaran.S
Jdom CRUD Operations
Here is sample code to do some operations like
//Adding elements
public void addElementAtXpath() {
try {
XPath xpath = XPath.newInstance("publishers");
xpath.addNamespace(root_.getNamespace());
Element publisherNode = (Element) xpath.selectSingleNode(root_);
org.jdom.Element element = new Element("kalees");
element.addContent(createElement(CI_FILE_RELEASE_OVERRIDE_AUTH_NODE, TRUE));
publisherNode.addContent(element);
} catch (Exception e) {
e.printStackTrace();
}
}
// Changing attributes
public void changeAttributeValue() {
try {
XPath xpath = XPath.newInstance("scm");
xpath.addNamespace(root_.getNamespace());
Element scmNode = (Element) xpath.selectSingleNode(root_);
scmNode.setAttribute("class", "Kalees");
} catch (Exception e) {
e.printStackTrace();
}
}
//Deleting Elements
public void deleteElementAtXpath() {
//deleting a node...
// deleting content of a node
//triggerNode.removeContent();
try {
String childName = "hudson.plugins.emailext.ExtendedEmailPublisher";
XPath xpath = XPath.newInstance("publishers");
xpath.addNamespace(root_.getNamespace());
Element triggerNode = (Element) xpath.selectSingleNode(root_); // if it is null then element is not present
triggerNode.removeChild(childName);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Element createElement(String nodeName, String NodeValue) {
org.jdom.Element element = new Element(nodeName);
if (NodeValue != null) {
element.addContent(NodeValue);
}
return element;
}
--
Thank you.
Regards,
Kaleeswaran.S
Wednesday, August 8, 2012
Eclisep Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.util.Arrays.copyOf
Eclisep Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.util.Arrays.copyOf
If you face this issue in eclipse while running a standalone java application. Eclipse will allocate only 384 MB of Java heap memory. based on the type of JRE you are using and eclipse plugin and application size, this issue occurs. we have to increase heap memory size. To increase the available heap memory, right click your java progrram click "run as -> run configurations" . click argument tab and type -Xms1024M -Xmx1024M in VM arguments field. This increases VM size while running from eclipse.
--
Thank you.
Regards,
Kaleeswaran.S
If you face this issue in eclipse while running a standalone java application. Eclipse will allocate only 384 MB of Java heap memory. based on the type of JRE you are using and eclipse plugin and application size, this issue occurs. we have to increase heap memory size. To increase the available heap memory, right click your java progrram click "run as -> run configurations" . click argument tab and type -Xms1024M -Xmx1024M in VM arguments field. This increases VM size while running from eclipse.
--
Thank you.
Regards,
Kaleeswaran.S
Thursday, August 2, 2012
JDOM - How to insert an element, after another
JDOM - How to insert an element, after another
private Document document_ = null;
private Element root_ = null;
@Test
public void generateXml() {
//Url path of the file
String filePath = "http://172.................. .xml";
testXmlGeneration(filePath);
}
private void testXmlGeneration(String url) {
try {
SAXBuilder builder = new SAXBuilder();
// giving xml file url to jdom for parsing
document_ = builder.build(url);
root_ = document_.getRootElement();
// creating necessary elements which is to be appended at a particular point of the location in xml
org.jdom.Element element = new Element("release");
element.addContent(createElement("override", "true"));
element.addContent(createElement("url", "http://outside.out.coml"));
element.addContent(createElement("username", "kaleeswaran"));
element.addContent(createElement("password", "xm0QczqM6k9x10Os1z0k="));
element.addContent(createElement("rpackage", "PackageRed"));
element.addContent(createElement("release", "release"));
element.addContent(createElement("file__patterns", null).addContent(createElement("hudson.plugins.collabnet.documentuploader.FilePattern", "*.zip")));
// i want to add the generated elements after the publish tag in the xml
XPath xpath = XPath.newInstance("publish");
xpath.addNamespace(root_.getNamespace());
Element pullisherNode = (Element) xpath.selectSingleNode(root_);
// here specify the index value where the element should be added and pass the element
pullisherNode.getContent().add(3, element);
File dest = new File("/Users/kaleeswaran/Desktop/gonfig.xml");
InputStream configAsStream = getConfigAsStream();
ciManager.streamToFile(dest, configAsStream) ;
System.out.println("new value added =======> " + url);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Element createElement(String nodeName, String NodeValue) {
org.jdom.Element element = new Element(nodeName);
if (NodeValue != null) {
element.addContent(NodeValue);
}
return element;
}
public InputStream getConfigAsStream() throws IOException {
XMLOutputter xmlOutput = new XMLOutputter();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
xmlOutput.output(document_, outputStream);
return new ByteArrayInputStream(outputStream.toByteArray());
}
--
Thank you.
Regards,
Kaleeswaran.S
Subscribe to:
Posts (Atom)