NFB Software Java-XML

The NFB Software Java-XML was created to help developers deal with common problems when dealing with String data in the forms of raw strings and XML files.

License

License

Categories

Categories

Java Languages
GroupId

GroupId

com.nfbsoftware
ArtifactId

ArtifactId

java-xml
Last Version

Last Version

1.0.12
Release Date

Release Date

Type

Type

jar
Description

Description

NFB Software Java-XML
The NFB Software Java-XML was created to help developers deal with common problems when dealing with String data in the forms of raw strings and XML files.
Project URL

Project URL

http://www.nfbsoftware.com
Source Code Management

Source Code Management

https://github.com/bclemenzi/java-xml

Download java-xml

How to add to project

<!-- https://jarcasting.com/artifacts/com.nfbsoftware/java-xml/ -->
<dependency>
    <groupId>com.nfbsoftware</groupId>
    <artifactId>java-xml</artifactId>
    <version>1.0.12</version>
</dependency>
// https://jarcasting.com/artifacts/com.nfbsoftware/java-xml/
implementation 'com.nfbsoftware:java-xml:1.0.12'
// https://jarcasting.com/artifacts/com.nfbsoftware/java-xml/
implementation ("com.nfbsoftware:java-xml:1.0.12")
'com.nfbsoftware:java-xml:jar:1.0.12'
<dependency org="com.nfbsoftware" name="java-xml" rev="1.0.12">
  <artifact name="java-xml" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.nfbsoftware', module='java-xml', version='1.0.12')
)
libraryDependencies += "com.nfbsoftware" % "java-xml" % "1.0.12"
[com.nfbsoftware/java-xml "1.0.12"]

Dependencies

compile (3)

Group / Artifact Type Version
commons-lang : commons-lang jar 2.3
xerces : xercesImpl jar 2.11.0
xalan : xalan jar 2.7.2

test (1)

Group / Artifact Type Version
junit : junit jar 3.8.1

Project Modules

There are no modules declared in this project.

java-xml

Java-XML is Java library designed to help simplify the building and parsing of XML documents

Features

  • XML is maintained in memory to prevent the over creation of document objects.
  • Support for creating new XML document objects
  • Support for importing existing XML documents from your file system
  • Support for importing existing XML documents from a String
  • Support for adding/updating child elements and their attributes
  • Support for handling CDATA sections; including the creation and reading of those elements
  • Support for getting a handle to an element via index, name, or XPath query
  • Support for removing elements via index, name, or XPath query

Getting started

Including the JAR in your project

The easiest way to incorporate the JAR into your Java project is to use Maven. Simply add a new dependency to your pom.xml:

<dependency>
  <groupId>com.nfbsoftware</groupId>
  <artifactId>java-xml</artifactId>
  <version>1.0.12</version>
</dependency>

Usage

The easiest way to get started is to just use the XmlDocument class, like this:

IXmlDocument doc = new XmlDocument();
IXmlElement testElement = doc.createChild("Test", "");
        
testElement.createChild("TestOne", "something");
testElement.createChild("TestTwo", "something else");
testElement.createChild("TestThree", "yet another something");

System.out.println(doc.toString());

Getting a handle to an existing element

IXmlDocument doc = new XmlDocument();
IXmlElement testElement = doc.createChild("Test", "");
        
testElement.createChild("TestOne", "something");
testElement.createChild("TestTwo", "something else");
testElement.createChild("TestThree", "yet another something");

String elementValue = testElement.getChildValue("TestOne");
        
System.out.println("elementValue: " + elementValue);

Creating an attribute on an element

IXmlDocument doc = new XmlDocument();
IXmlElement testElement = doc.createChild("Test", "");
        
testElement.createChild("TestOne", "something");
testElement.createChild("TestTwo", "something else");
        
IXmlElement thirdElement = testElement.createChild("TestThree", "yet another something");
thirdElement.setAttribute("ID", "123456789");
        
System.out.println(doc.toString());

Get an element using xPath

StringBuffer xmlString = new StringBuffer();
xmlString.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
xmlString.append("<ROOT>");
xmlString.append("  <one>");
xmlString.append("      <two>");
xmlString.append("          <three>test3</three>");
xmlString.append("      </two>");
xmlString.append("  </one>");
xmlString.append("</ROOT>");
            
IXmlDocument doc = new XmlDocument(xmlString.toString());
IXmlElement root = doc.getRootElement();
            
// Make an XPath call to get a handle on the third node.
IXmlElement nodeThree = root.getChild("one/two/three");
            
System.out.println("thirdValue: " + nodeThree.getValue());

Select/search for elements using xPath

StringBuffer xmlString = new StringBuffer();
xmlString.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
xmlString.append("<ROOT>");
xmlString.append("  <one>");
xmlString.append("      <two>");
xmlString.append("          <element id=\"1\">Element-100</element>");
xmlString.append("          <element id=\"2\">Element-200</element>");
xmlString.append("          <element id=\"3\">Element-300</element>");
xmlString.append("      </two>");
xmlString.append("  </one>");
xmlString.append("</ROOT>");
            
IXmlDocument doc = new XmlDocument(xmlString.toString());
IXmlElement root = doc.getRootElement();
            
// Make an XPath call to get a handle on children with a matching xpath
List<IXmlElement> elements = root.selectChildren("//element");
            
List<String> elementValues = new ArrayList<String>();
for(IXmlElement tmpElement : elements)
{
	String tmpValue = tmpElement.getValue();
                
    System.out.println("tmpValue: " + tmpValue);
}

Select/search for a single element using xPath. Exception is throw if multiple are found.

StringBuffer xmlString = new StringBuffer();
xmlString.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
xmlString.append("<ROOT>");
xmlString.append("  <one>");
xmlString.append("      <two>");
xmlString.append("          <element id=\"1\">Element-100</element>");
xmlString.append("          <element id=\"2\">Element-200</element>");
xmlString.append("          <element id=\"3\">Element-300</element>");
xmlString.append("      </two>");
xmlString.append("  </one>");
xmlString.append("</ROOT>");
            
IXmlDocument doc = new XmlDocument(xmlString.toString());
IXmlElement root = doc.getRootElement();
            
// Make an XPath call to get a handle to the child using an xpath query.  An exception will be thrown if multiple matches are found.
IXmlElement tmpElement = root.selectChild("//element[@id='2']");
                       
System.out.println("tmpValue: " + tmpElement.getValue());

Get a CDATA Section

StringBuffer xmlString = new StringBuffer();
xmlString.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
xmlString.append("<ROOT>");
xmlString.append("  <one>");
xmlString.append("      <column>");
xmlString.append("          <name>order</name>");
xmlString.append("          <value><![CDATA[1234]]></value>");
xmlString.append("      </column>");
xmlString.append("  </one>");
xmlString.append("</ROOT>");
            
IXmlDocument doc = new XmlDocument(xmlString.toString());
IXmlElement root = doc.getRootElement();
            
// Make an XPath call to get a handle on the column element
IXmlElement columnNode = root.getChild("one/column");
            
String cdataValue = columnNode.getCDATASection("value");

Set a CDATA Section

IXmlDocument doc = new XmlDocument();
IXmlElement testElement = doc.createChild("Test", "");
            
testElement.createChild("TestOne", "something");
testElement.createChild("TestTwo", "something else");
            
IXmlElement cdataElement = testElement.createChild("TestCDATA", "");
cdataElement.setCDATASection("MYCDATATEST");
            
System.out.println(doc.toString());

Creating an XmlDocument from a string

StringBuffer xmlString = new StringBuffer();
xmlString.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
xmlString.append("<ROOT>");
xmlString.append("  <one>test1</one>");
xmlString.append("  <two>");
xmlString.append("      <three>test3</three>");
xmlString.append("  </two>");
xmlString.append("</ROOT>");
            
IXmlDocument doc = new XmlDocument(xmlString.toString());
IXmlElement root = doc.getRootElement();
            
IXmlElement nodeTwo = root.getChild("two");
IXmlElement nodeThree = nodeTwo.getChild("three");
            
String thirdValue = nodeThree.getValue();
            
System.out.println("thirdValue: " + thirdValue);

Creating an XmlDocument from a file

File myXmlFile = new File("my-xml-file.xml");
IXmlDocument doc = new XmlDocument(myXmlFile);
IXmlElement testElement = doc.createChild("Test", "");
        
testElement.createChild("TestOne", "something");
testElement.createChild("TestTwo", "something else");
testElement.createChild("TestThree", "yet another something");

String elementValue = testElement.getChildValue("TestOne");
        
System.out.println("elementValue: " + elementValue);

Remove an XmlElement

IXmlDocument doc = new XmlDocument();
IXmlElement testElement = doc.createChild("Test", "");
        
testElement.createChild("TestOne", "something");
testElement.createChild("TestTwo", "something else");
testElement.createChild("TestThree", "yet another something");
        
// Delete element
testElement.removeChild("TestOne");
        
System.out.println(testElement.toString());

Remove an XmlElement using XPath

StringBuffer xmlString = new StringBuffer();
xmlString.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
xmlString.append("<ROOT>");
xmlString.append("  <one>");
xmlString.append("      <two>");
xmlString.append("          <element id=\"1\">Element-100</element>");
xmlString.append("          <element id=\"2\">Element-200</element>");
xmlString.append("          <element id=\"3\">Element-300</element>");
xmlString.append("      </two>");
xmlString.append("  </one>");
xmlString.append("</ROOT>");
        
IXmlDocument doc = new XmlDocument(xmlString.toString());
IXmlElement root = doc.getRootElement();
        
// Delete element
root.removeChildWithXpath("//element[@id='2']");
        
System.out.println(root.toString());

Remove multiple XmlElements using XPath

StringBuffer xmlString = new StringBuffer();
xmlString.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
xmlString.append("<ROOT>");
xmlString.append("  <one>");
xmlString.append("      <two>");
xmlString.append("          <element id=\"1\">Element-100</element>");
xmlString.append("          <element id=\"2\">Element-200</element>");
xmlString.append("          <element id=\"3\">Element-300</element>");
xmlString.append("      </two>");
xmlString.append("      <three>");
xmlString.append("          <element id=\"1\">Element-101</element>");
xmlString.append("          <element id=\"2\">Element-201</element>");
xmlString.append("          <element id=\"3\">Element-301</element>");
xmlString.append("      </three>");
xmlString.append("  </one>");
xmlString.append("</ROOT>");
        
IXmlDocument doc = new XmlDocument(xmlString.toString());
IXmlElement root = doc.getRootElement();
        
// Delete element
root.removeChildrenWithXpath("//element[@id='2']");
        
System.out.println(root.toString());

Versions

Version
1.0.12
1.0.10
1.0.9
1.0.8
1.0.7
1.0.6
1.0.5