Package org.codehaus.cargo.util
Class XmlUtils
- java.lang.Object
-
- org.codehaus.cargo.util.XmlUtils
-
public class XmlUtils extends Object
This class offers utility methods for handling XML files.
-
-
Constructor Summary
Constructors Constructor Description XmlUtils()
default constructor will assign no namespaces and use a default file handler.XmlUtils(boolean namespaceAware)
default constructor will assign no namespaces and use a default file handler.XmlUtils(FileHandler fileHandler)
constructor will assign no namespaces.XmlUtils(FileHandler fileHandler, boolean namespaceAware)
constructor will assign no namespaces.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Document
createDocument()
Creates a new, blank XML document.FileHandler
getFileHandler()
Map<String,String>
getNamespaces()
Document
loadXmlFromFile(String sourceFile)
read the specified file into a Document.Element
parseIntoElement(String elementToParse)
parse the passed string into anElement
object.void
saveXml(Document document, String filename)
write the xml document to disk, rethrowing checked exceptions as runtime.Element
selectElementMatchingXPath(String xpath, Element toSearch)
The following will search the given element for the specified XPath and return any node that matches.List<Element>
selectElementsMatchingXPath(String xpath, Element toSearch)
The following will search the given element for the specified XPath and return a list of nodes that match.void
setFileHandler(FileHandler fileHandler)
void
setNamespaces(Map<String,String> namespaces)
String
toString(Element node)
Output an XML node as string, without the XML header.
-
-
-
Constructor Detail
-
XmlUtils
public XmlUtils()
default constructor will assign no namespaces and use a default file handler.
-
XmlUtils
public XmlUtils(boolean namespaceAware)
default constructor will assign no namespaces and use a default file handler.- Parameters:
namespaceAware
- true if XmlUtils should be namespace aware.
-
XmlUtils
public XmlUtils(FileHandler fileHandler)
constructor will assign no namespaces.- Parameters:
fileHandler
- used to read and write xml files.
-
XmlUtils
public XmlUtils(FileHandler fileHandler, boolean namespaceAware)
constructor will assign no namespaces.- Parameters:
fileHandler
- used to read and write xml files.namespaceAware
- true if XmlUtils should be namespace aware.
-
-
Method Detail
-
selectElementsMatchingXPath
public List<Element> selectElementsMatchingXPath(String xpath, Element toSearch)
The following will search the given element for the specified XPath and return a list of nodes that match.- Parameters:
xpath
- - selection criteriatoSearch
- - element to start the search at- Returns:
- List of matching elements
-
selectElementMatchingXPath
public Element selectElementMatchingXPath(String xpath, Element toSearch)
The following will search the given element for the specified XPath and return any node that matches.- Parameters:
xpath
- - selection criteriatoSearch
- - element to start the search at- Returns:
- a matching element
-
loadXmlFromFile
public Document loadXmlFromFile(String sourceFile)
read the specified file into a Document.- Parameters:
sourceFile
- file to read- Returns:
- Document corresponding with sourceFile
-
saveXml
public void saveXml(Document document, String filename)
write the xml document to disk, rethrowing checked exceptions as runtime.- Parameters:
document
- document to write to diskfilename
- where to write the document
-
getFileHandler
public FileHandler getFileHandler()
- Returns:
- the Cargo file utility class
-
setFileHandler
public void setFileHandler(FileHandler fileHandler)
- Parameters:
fileHandler
- the Cargo file utility class to use. This method is useful for unit testing with Mock objects as it can be passed a test file handler that doesn't perform any real file action.
-
setNamespaces
public void setNamespaces(Map<String,String> namespaces)
- Parameters:
namespaces
- the namespaces to set
-
parseIntoElement
public Element parseIntoElement(String elementToParse)
parse the passed string into anElement
object.- Parameters:
elementToParse
- string to parse- Returns:
- result of parsing
-
createDocument
public Document createDocument()
Creates a new, blank XML document.- Returns:
- New, blank XML document.
-
-