public class XmlElement extends Object implements XmlContent
Constructor and Description |
---|
XmlElement(String name)
Creates an XmlElement instance with the given name.
|
Modifier and Type | Method and Description |
---|---|
XmlElement |
addAttribute(String name,
String value)
Deprecated.
This method is deprecated. Use
setAttribute method
instead. |
XmlCDATA |
addCDATA(String text)
Adds a new XmlCDATA text to the current XmlElement.
|
XmlElement |
addChild(XmlContent child)
Adds the given XmlContent to the current to the current XmlElement
instance.
|
XmlComment |
addComment(String text)
Adds a new XmlComment to the current XmlElement.
|
XmlElement |
addElement(String name)
Adds a new XmlElement to the current XmlElement.
|
XmlText |
addText(String text)
Adds a new XmlText content to the current XmlElement.
|
static XmlElement |
create(String name)
Creates a new XmlElement.
|
boolean |
deepEquals(XmlElement o)
Compare the actual XmlElement along whit it's childs.
|
boolean |
equals(Object o)
Two XmlElements are equals if they have the same name, the same number of
attributes and all those attributes contains the same values.
|
String |
getAttribute(String name)
Gets the named attribute value.
|
Map<String,String> |
getAttributes()
Gets the attribute map of the current XmlElement.
|
XmlContent |
getChild(int index)
Gets the child XmlContent at the given index of the current XmlElement.
|
List<XmlContent> |
getChilds()
Gets this XmlContent instance child list.
|
XmlElement |
getElement(String name)
Return the first occurence of the named child XmlElement.
|
XmlElement |
getElement(String name,
int index)
Gets the named child element at the given index.
|
List<XmlElement> |
getElements()
Gets a list of all XmlElement children.
|
List<XmlElement> |
getElements(String name)
Returns all child XmlElement having the given name.
|
long |
getId()
Gets the Id of the current node.
|
String |
getName()
Gets the name of the current XmlElement.
|
String |
getValue(String elementName)
Concatenate all child's XmlContent.toString() trimmed and separated by a
single space of the named XmlElement.
|
boolean |
hasAttribute(String name)
Tells if the given attribute name exists in the current XmlElement.
|
int |
hashCode() |
static XmlElement |
readXML(File file)
Reads an XmlElement from the given file.
|
static XmlElement |
readXML(Reader reader)
Reads an XmlElement from the given reader.
|
static XmlElement |
readXML(Reader reader,
XmlContentListener listener)
Read XML from the given reader and fire events from the provided
listener.
|
static XmlElement |
readXML(String xml)
Parse the given xml string into an XmlElement.
|
XmlElement |
setAttribute(String name,
String value)
Sets a value to the given attribute.
|
void |
setName(String name)
Sets the name of the current element.
|
String |
toString() |
String |
toXml()
Method transforming the current class into its XML string representation.
|
void |
write(Writer writer)
Method defining how the current class will write itself as XML to the
given writer.
|
public XmlElement(String name)
name
- The name given to the XmlElement.public static XmlElement create(String name)
name
- The name of the node to create.public static XmlElement readXML(File file) throws XmlException
file
- The file containing the XmlElement.XmlException
- If there is any problem during Xml parsing.public static XmlElement readXML(Reader reader) throws XmlException
reader
- The reader containing the XmlElement.XmlException
- If there is any problem during Xml parsing.public static XmlElement readXML(Reader reader, XmlContentListener listener) throws XmlException
reader
- The reader containing the XML data.listener
- The XmlContentListener containing code to be notified each
time an Xml content object is created.XmlException
- Thrown if a problem occur while reading the provided XML
data.public static XmlElement readXML(String xml) throws XmlException
xml
- The XML string to parse.XmlException
- Thrown if a problem occur while reading the provided XML
data.@Deprecated public XmlElement addAttribute(String name, String value)
setAttribute
method
instead.name
- The name of the attribute.value
- The value of the attribute.public XmlCDATA addCDATA(String text)
text
- The CDATA text.public XmlElement addChild(XmlContent child)
child
- The child XmlContent to add.public XmlComment addComment(String text)
text
- The text of the XmlComment.public XmlElement addElement(String name)
name
- The name of the XmlElement.public XmlText addText(String text)
text
- The text of the XmlText.public boolean deepEquals(XmlElement o)
o
- The other element to do the comparison with.public boolean equals(Object o)
public boolean hasAttribute(String name)
name
- The name of the seeked attribute.public String getAttribute(String name)
name
- The name of the desired attribute.public Map<String,String> getAttributes()
public XmlContent getChild(int index)
index
- The index of the child to get.IndexOutOfBoundsException
- if the index is out of range.public List<XmlContent> getChilds()
public String getValue(String elementName)
for example:
given the following XML hierarchy: - root - child - "string value"
Executing:
root.getValue("child")
Will return:
"string value"
elementName
- The name of the child element for which we want the inner
value.public XmlElement getElement(String name)
name
- The name of the desired child element.public XmlElement getElement(String name, int index)
name
- The name of the child XmlElement desired.index
- The index of the desired XmlElement.public List<XmlElement> getElements(String name)
name
- The name of the desired child XmlElementspublic List<XmlElement> getElements()
public long getId()
XmlContent
getId
in interface XmlContent
public String getName()
public XmlElement setAttribute(String name, String value)
name
- The name of the attribute.value
- The value of the attribute.public void setName(String name)
name
- The new name of the current attribute.public String toXml()
XmlContent
toXml
in interface XmlContent
public void write(Writer writer) throws IOException
XmlContent
write
in interface XmlContent
writer
- Thw writer to write to.IOException
- Thrown if an error occurs while writing to the writer.Copyright © 2015. All rights reserved.