Giter Site home page Giter Site logo

xml-wrappers-for-java's Introduction

XML Wrappers for Java

A lightweight set of wrappers around the Java DOM XML classes.

Send complaints, suggestions, and thanks to [email protected]

Maven

This project uses Sonatype's OSS Nexus hosting to sync to Maven central.

<dependencies>
    ...
    <dependency>
        <groupId>com.jeffrodriguez</groupId>
        <artifactId>xmlwrapper</artifactId>
        <version>2.1.0</version>
    </dependency>
    ...
</dependencies>

Versioning

For transparency and insight into our release cycle, and for striving to maintain backward compatibility, XML Wrapper will be maintained under the Semantic Versioning guidelines as much as possible.

Releases will be numbered with the follow format:

<major>.<minor>.<patch>

And constructed with the following guidelines:

  • Breaking backward compatibility bumps the major
  • New additions without breaking backward compatibility bumps the minor
  • Bug fixes and misc changes bump the patch

For more information on SemVer, please visit http://semver.org/.

Creating an XML Instance

Wrap an existing document

XML xml = new XML(document);

Parse a string

XML xml = XML.parse("<?xml version=\"1.0\"?><foo/>");

Create from scratch

XML xml = XML.create("foo");

Clone an instance

XML clone = xml.clone();

Outputting XML

Pretty formatting

xml.toString(true);

Compact formatting

xml.toString(false);

Working with Elements

Getting the document element

XMLElement root = xml.getRoot();

Adding children to an element

root.addChild("bar"); // foo -> bar
root.addChild("baz"); // foo -> bar
                      //     -> baz

Chaining

root.addChild("bar").addChild("baz"); // foo -> bar -> baz

Navigating

root.getChild("bar").getParent(); // foo

Iterating

// Enhanced for-loop
for (XMLElement child : root.getChildren("bar")) {
    // Do something with child
}

// Traditional iterator
Iterator<XMLElement> it = root.getChildren("bar").iterator();
while (it.hasNext()) {
    XMLElement child = it.next();
    // Do something with child
}

Element Text

// String
root.setValue("bar");
root.getValue();      // "bar"

// Integer
root.setValue("1");
root.getValueAsInteger(); // 1

// Long
root.setValue("1");
root.getValueAsLong(); // 1L

// Shorthand
root.setChildValue("bar", "baz"); // <bar>baz</bar>
root.getChildValue("bar");        // baz

Attributes

XMLElement element = xml.getRoot();
element.setAttribute("bar", "baz");
element.getAttribute("bar");        // "baz"

XPath Support

The xpathElements("...") method allows you to use XPath expressions on your document:

// Enhanced for-loop
for (XMLElement child : xml.xpathElements("//bar")) {
    // Do something with child
}

// Traditional iterator
Iterator<XMLElement> it = xml.xpathElements("//bar").iterator();
while (it.hasNext()) {
    XMLElement child = it.next();
    // Do something with child
}

xml-wrappers-for-java's People

Contributors

jeffreyrodriguez avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.