Package com.tridion.util.xml
Class XMLUtils
java.lang.Object
com.tridion.util.xml.XMLUtils
Various utility methods for manipulating XML.
-
Method Summary
Modifier and TypeMethodDescriptionconvertContentToDocument(byte[] content) Convert a byte[] array to a List of DOM objects.static StringconvertDocumentToString(Document document) Converts the passedDocumentto aString.static ObjectRemoves XML characters ('&', ''', '"', '<' and '>') from passed content.static StringgetAttributeValue(Document parent, String tagName, String attributeName) static StringgetAttributeValue(Element parent, String tagName, String attributeName) static ElementgetElement(Document doc, String tagName, int index) Return an Element given an XML document, tag name, and index.static ElementgetFirstElement(Element parent, String tagName) static intReturn the number of person in an XML document.static StringGiven a person element, must get the element specified by the tagName, then must traverse that Node to get the value.static ElementloadXML(InputStream xmlStream) Passed xml string will be loaded if passed string is well formed xml.static voidprintNodeTypes(NodeList rows) For testing purpose, it print out Node list.static StringremoveBOMForUTF8(String content) Removes BOM characters for UTF-8 files.static StringremoveNonAlphaNumericChars(String originalString) Removes everything except letters and numbers from the provided String.
-
Method Details
-
getElement
Return an Element given an XML document, tag name, and index.- Parameters:
doc- XML docuemnttagName- a tag nameindex- a index- Returns:
- an Element, or null in case the element could not be found.
-
getSize
Return the number of person in an XML document.- Parameters:
doc- XML documenttagName- a tag name- Returns:
- the number of person in an XML document
-
getValue
Given a person element, must get the element specified by the tagName, then must traverse that Node to get the value. Step1) get Element of name tagName from e Step2) cast element to Node and then traverse it for its non-whitespace, cr/lf value. Step3) return it!NOTE: Element is a subclass of Node
- Parameters:
e- an ElementtagName- a tag name- Returns:
- s the value of a Node
-
getFirstElement
-
getAttributeValue
- Parameters:
parent- an ElementtagName- a tag name- Returns:
- the value of an attribute
-
getAttributeValue
- Parameters:
parent- an DocumenttagName- a tag name- Returns:
- the value of an attribute
-
printNodeTypes
For testing purpose, it print out Node list.- Parameters:
rows- a Nodelist
-
convertDocumentToString
Converts the passedDocumentto aString.- Parameters:
document- TheDocumentto convert.- Returns:
- An XML string representation of the
Document.
-
convertContentToDocument
public static List<Object> convertContentToDocument(byte[] content) throws ParserConfigurationException, SAXException, IOException Convert a byte[] array to a List of DOM objects. The converter ignores the root node.- Parameters:
content- All the XML nodes that will be converted to DOM.- Returns:
- List of DOM.
- Throws:
ParserConfigurationExceptionSAXExceptionIOException
-
removeBOMForUTF8
Removes BOM characters for UTF-8 files.- Parameters:
content- Content of the file that might contain BOM.- Returns:
- Content without BOM.
-
escape
Removes XML characters ('&', ''', '"', '<' and '>') from passed content.- Parameters:
content- The content before escaping.- Returns:
- Content after escaping xml characters
-
loadXML
Passed xml string will be loaded if passed string is well formed xml.- Parameters:
xmlStream- xml as a stream- Returns:
- Document object if passed xml is well formed otherwise null
-
removeNonAlphaNumericChars
Removes everything except letters and numbers from the provided String.- Parameters:
originalString- the original string.- Returns:
- the processed string.
-