Last active
December 1, 2015 12:15
-
-
Save donv/ed8b296ccfc99f927f71 to your computer and use it in GitHub Desktop.
JRuby Xerces example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gem 'nokogiri' | |
import java.lang.Integer | |
import java.lang.System | |
import java.io.IOException | |
import java.util.ArrayList | |
import java.util.Iterator | |
import java.util.List | |
import javax.xml.parsers.DocumentBuilder | |
import javax.xml.parsers.DocumentBuilderFactory | |
import javax.xml.parsers.ParserConfigurationException | |
import org.w3c.dom.Document | |
import org.w3c.dom.Element | |
import org.w3c.dom.NodeList | |
import org.xml.sax.SAXException | |
class DomParserExample | |
def initialize | |
@myEmpls = ArrayList.new | |
end | |
def runExample | |
parseXmlFile | |
parseDocument | |
printData | |
end | |
private | |
def parseXmlFile | |
dbf = DocumentBuilderFactory.newInstance | |
begin | |
db = dbf.newDocumentBuilder | |
@dom = db.parse("employees.xml") | |
rescue ParserConfigurationException => pce | |
pce.printStackTrace | |
rescue SAXException => se | |
se.printStackTrace | |
rescue IOException => ioe | |
ioe.printStackTrace | |
end | |
end | |
def parseDocument | |
docEle = @dom.getDocumentElement | |
nl = docEle.getElementsByTagName("Employee") | |
if nl != nil && nl.getLength() > 0 | |
i = 0 | |
while i < nl.getLength() | |
el = nl.item(i) | |
e = getEmployee(el) | |
@myEmpls.add(e) | |
i+= 1 | |
end | |
end | |
end | |
# I take an employee element and read the values in, create | |
# an Employee object and return it | |
# @param empEl | |
# @return | |
def getEmployee(empEl) | |
name = getTextValue(empEl,"Name") | |
id = getIntValue(empEl,"Id") | |
age = getIntValue(empEl,"Age") | |
type = empEl.getAttribute("type") | |
e = {name: name,id: id, age: age, type: type} | |
return e | |
end | |
# I take a xml element and the tag name, look for the tag and get | |
# the text content | |
# i.e for <employee><name>John</name></employee> xml snippet if | |
# the Element points to employee node and tagName is name I will return John | |
# @param ele | |
# @param tagName | |
# @return | |
def getTextValue(ele, tagName) | |
textVal = nil | |
nl = ele.getElementsByTagName(tagName) | |
if nl != nil && nl.getLength() > 0 | |
el = nl.item(0) | |
textVal = el.getFirstChild().getNodeValue() | |
end | |
return textVal; | |
end | |
# Calls getTextValue and returns a int value | |
# @param ele | |
# @param tagName | |
# @return | |
def getIntValue(ele, tagName) | |
return Integer.parseInt(getTextValue(ele,tagName)) | |
end | |
# Iterate through the list and print the | |
# content to console | |
def printData | |
System.out.println("No of Employees '" + @myEmpls.size.to_s + "'.") | |
it = @myEmpls.iterator | |
while it.hasNext | |
System.out.println(it.next.to_s) | |
end | |
end | |
end | |
dpe = DomParserExample.new | |
dpe.runExample |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<Personnel> | |
<Employee type='perma"nent'> | |
<Name>Seagull</Name> | |
<Id>3674</Id> | |
<Age>34</Age> | |
</Employee> | |
<Employee type="contract"> | |
<Name>Robin</Name> | |
<Id>3675</Id> | |
<Age>25</Age> | |
</Employee> | |
<Employee type="permanent"> | |
<Name>Crow</Name> | |
<Id>3676</Id> | |
<Age>28</Age> | |
</Employee> | |
</Personnel> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment