Class JniYXmlElement
- All Implemented Interfaces:
AutoCloseable,net.carcdr.ycrdt.YXmlElement,net.carcdr.ycrdt.YXmlNode
YXmlElement provides collaborative operations for XML elements including tag names and attributes. Multiple users can modify the same element simultaneously, and changes will be merged automatically using CRDT algorithms.
This class implements Closeable and should be used with try-with-resources
to ensure proper cleanup of native resources.
Example usage:
try (YDoc doc = new JniYDoc();
YXmlElement element = doc.getXmlElement("div")) {
element.setAttribute("class", "container");
element.setAttribute("id", "main");
System.out.println(element.getTag()); // div
System.out.println(element.getAttribute("class")); // container
}
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface net.carcdr.ycrdt.YXmlNode
net.carcdr.ycrdt.YXmlNode.NodeType -
Method Summary
Modifier and TypeMethodDescriptionintGets the number of child nodes in this element.intchildCount(net.carcdr.ycrdt.YTransaction txn) Gets the number of child nodes in this element using an existing transaction.voidclose()Closes this YXmlElement and releases native resources.getAttribute(String name) Gets an attribute value by name.getAttribute(net.carcdr.ycrdt.YTransaction txn, String name) Gets an attribute value by name using an existing transaction.String[]Gets all attribute names.String[]getAttributeNames(net.carcdr.ycrdt.YTransaction txn) Gets all attribute names using an existing transaction.getChild(int index) Gets the child node at the specified index.getChild(net.carcdr.ycrdt.YTransaction txn, int index) Gets the child node at the specified index using an existing transaction.intGets the index of this element within its parent's children.intgetIndexInParent(net.carcdr.ycrdt.YTransaction txn) Gets the index of this element within its parent's children using an existing transaction.net.carcdr.ycrdt.YXmlNode.NodeTypeGets the parent of this XML element.getParent(net.carcdr.ycrdt.YTransaction txn) Gets the parent of this XML element using an existing transaction.getTag()Returns the tag name of this XML element.getTag(net.carcdr.ycrdt.YTransaction txn) Returns the tag name of this XML element using an existing transaction.insertElement(int index, String tag) Inserts an XML element child at the specified index.insertElement(net.carcdr.ycrdt.YTransaction txn, int index, String tag) Inserts an XML element child at the specified index within an existing transaction.insertText(int index) Inserts an XML text child at the specified index.insertText(net.carcdr.ycrdt.YTransaction txn, int index) Inserts an XML text child at the specified index within an existing transaction.booleanisClosed()Checks if this YXmlElement has been closed.net.carcdr.ycrdt.YSubscriptionobserve(net.carcdr.ycrdt.YObserver observer) Registers an observer to be notified when this XML element changes.voidremoveAttribute(String name) Removes an attribute.voidremoveAttribute(net.carcdr.ycrdt.YTransaction txn, String name) Removes an attribute within an existing transaction.voidremoveChild(int index) Removes the child node at the specified index.voidremoveChild(net.carcdr.ycrdt.YTransaction txn, int index) Removes the child node at the specified index within an existing transaction.voidsetAttribute(String name, String value) Sets an attribute value.voidsetAttribute(net.carcdr.ycrdt.YTransaction txn, String name, String value) Sets an attribute value within an existing transaction.toString()Returns the XML string representation of this element.toString(net.carcdr.ycrdt.YTransaction txn) Returns the XML string representation of this element using an existing transaction.voidunobserveById(long subscriptionId) Unregisters an observer by its subscription ID.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface net.carcdr.ycrdt.YXmlNode
asElement, asText
-
Method Details
-
getNodeType
public net.carcdr.ycrdt.YXmlNode.NodeType getNodeType()- Specified by:
getNodeTypein interfacenet.carcdr.ycrdt.YXmlNode
-
getTag
Returns the tag name of this XML element.- Specified by:
getTagin interfacenet.carcdr.ycrdt.YXmlElement- Returns:
- The tag name
- Throws:
IllegalStateException- if the XML element has been closed
-
getTag
Returns the tag name of this XML element using an existing transaction.- Specified by:
getTagin interfacenet.carcdr.ycrdt.YXmlElement- Parameters:
txn- Transaction handle- Returns:
- The tag name
- Throws:
IllegalArgumentException- if txn is nullIllegalStateException- if the XML element has been closed
-
getAttribute
Gets an attribute value by name.- Specified by:
getAttributein interfacenet.carcdr.ycrdt.YXmlElement- Parameters:
name- The attribute name- Returns:
- The attribute value, or null if not found
- Throws:
IllegalArgumentException- if name is nullIllegalStateException- if the XML element has been closed
-
getAttribute
Gets an attribute value by name using an existing transaction.- Specified by:
getAttributein interfacenet.carcdr.ycrdt.YXmlElement- Parameters:
txn- Transaction handlename- The attribute name- Returns:
- The attribute value, or null if not found
- Throws:
IllegalArgumentException- if txn or name is nullIllegalStateException- if the XML element has been closed
-
setAttribute
Sets an attribute value.- Specified by:
setAttributein interfacenet.carcdr.ycrdt.YXmlElement- Parameters:
name- The attribute namevalue- The attribute value- Throws:
IllegalArgumentException- if name or value is nullIllegalStateException- if the XML element has been closed
-
setAttribute
Sets an attribute value within an existing transaction.Use this method to batch multiple operations:
try (JniYTransaction txn = doc.beginTransaction()) { element.setAttribute(txn, "class", "container"); element.setAttribute(txn, "id", "main"); }- Specified by:
setAttributein interfacenet.carcdr.ycrdt.YXmlElement- Parameters:
txn- Transaction handlename- The attribute namevalue- The attribute value- Throws:
IllegalArgumentException- if txn, name or value is nullIllegalStateException- if the XML element has been closed
-
removeAttribute
Removes an attribute.- Specified by:
removeAttributein interfacenet.carcdr.ycrdt.YXmlElement- Parameters:
name- The attribute name to remove- Throws:
IllegalArgumentException- if name is nullIllegalStateException- if the XML element has been closed
-
removeAttribute
Removes an attribute within an existing transaction.Use this method to batch multiple operations:
try (JniYTransaction txn = doc.beginTransaction()) { element.removeAttribute(txn, "class"); element.removeAttribute(txn, "id"); }- Specified by:
removeAttributein interfacenet.carcdr.ycrdt.YXmlElement- Parameters:
txn- Transaction handlename- The attribute name to remove- Throws:
IllegalArgumentException- if txn or name is nullIllegalStateException- if the XML element has been closed
-
getAttributeNames
Gets all attribute names.- Specified by:
getAttributeNamesin interfacenet.carcdr.ycrdt.YXmlElement- Returns:
- An array of all attribute names
- Throws:
IllegalStateException- if the XML element has been closed
-
getAttributeNames
Gets all attribute names using an existing transaction.- Specified by:
getAttributeNamesin interfacenet.carcdr.ycrdt.YXmlElement- Parameters:
txn- Transaction handle- Returns:
- An array of all attribute names
- Throws:
IllegalArgumentException- if txn is nullIllegalStateException- if the XML element has been closed
-
toString
Returns the XML string representation of this element.- Specified by:
toStringin interfacenet.carcdr.ycrdt.YXmlElement- Overrides:
toStringin classObject- Returns:
- The XML string representation
- Throws:
IllegalStateException- if the XML element has been closed
-
toString
Returns the XML string representation of this element using an existing transaction.- Specified by:
toStringin interfacenet.carcdr.ycrdt.YXmlElement- Parameters:
txn- Transaction handle- Returns:
- The XML string representation
- Throws:
IllegalArgumentException- if txn is nullIllegalStateException- if the XML element has been closed
-
childCount
public int childCount()Gets the number of child nodes in this element.- Specified by:
childCountin interfacenet.carcdr.ycrdt.YXmlElement- Returns:
- The number of child nodes
- Throws:
IllegalStateException- if the XML element has been closed
-
childCount
public int childCount(net.carcdr.ycrdt.YTransaction txn) Gets the number of child nodes in this element using an existing transaction.- Specified by:
childCountin interfacenet.carcdr.ycrdt.YXmlElement- Parameters:
txn- Transaction handle- Returns:
- The number of child nodes
- Throws:
IllegalArgumentException- if txn is nullIllegalStateException- if the XML element has been closed
-
insertElement
Inserts an XML element child at the specified index.- Specified by:
insertElementin interfacenet.carcdr.ycrdt.YXmlElement- Parameters:
index- The index at which to insert the childtag- The tag name for the new element- Returns:
- The new child element
- Throws:
IllegalArgumentException- if tag is nullIndexOutOfBoundsException- if index is negativeIllegalStateException- if the XML element has been closed
-
insertElement
Inserts an XML element child at the specified index within an existing transaction.Use this method to batch multiple operations:
try (JniYTransaction txn = doc.beginTransaction()) { element.insertElement(txn, 0, "div"); element.insertElement(txn, 1, "span"); }- Specified by:
insertElementin interfacenet.carcdr.ycrdt.YXmlElement- Parameters:
txn- Transaction handleindex- The index at which to insert the childtag- The tag name for the new element- Returns:
- The new child element
- Throws:
IllegalArgumentException- if txn or tag is nullIndexOutOfBoundsException- if index is negativeIllegalStateException- if the XML element has been closed
-
insertText
Inserts an XML text child at the specified index.- Specified by:
insertTextin interfacenet.carcdr.ycrdt.YXmlElement- Parameters:
index- The index at which to insert the child- Returns:
- The new child text node
- Throws:
IndexOutOfBoundsException- if index is negativeIllegalStateException- if the XML element has been closed
-
insertText
Inserts an XML text child at the specified index within an existing transaction.Use this method to batch multiple operations:
try (JniYTransaction txn = doc.beginTransaction()) { element.insertText(txn, 0); element.insertText(txn, 1); }- Specified by:
insertTextin interfacenet.carcdr.ycrdt.YXmlElement- Parameters:
txn- Transaction handleindex- The index at which to insert the child- Returns:
- The new child text node
- Throws:
IllegalArgumentException- if txn is nullIndexOutOfBoundsException- if index is negativeIllegalStateException- if the XML element has been closed
-
getChild
Gets the child node at the specified index. The returned object can be either YXmlElement or YXmlText.- Specified by:
getChildin interfacenet.carcdr.ycrdt.YXmlElement- Parameters:
index- The index of the child to retrieve- Returns:
- The child node, or null if not found
- Throws:
IndexOutOfBoundsException- if index is negativeIllegalStateException- if the XML element has been closed
-
getChild
Gets the child node at the specified index using an existing transaction. The returned object can be either YXmlElement or YXmlText.- Specified by:
getChildin interfacenet.carcdr.ycrdt.YXmlElement- Parameters:
txn- Transaction handleindex- The index of the child to retrieve- Returns:
- The child node, or null if not found
- Throws:
IllegalArgumentException- if txn is nullIndexOutOfBoundsException- if index is negativeIllegalStateException- if the XML element has been closed
-
removeChild
public void removeChild(int index) Removes the child node at the specified index.- Specified by:
removeChildin interfacenet.carcdr.ycrdt.YXmlElement- Parameters:
index- The index of the child to remove- Throws:
IndexOutOfBoundsException- if index is negativeIllegalStateException- if the XML element has been closed
-
removeChild
public void removeChild(net.carcdr.ycrdt.YTransaction txn, int index) Removes the child node at the specified index within an existing transaction.Use this method to batch multiple operations:
try (JniYTransaction txn = doc.beginTransaction()) { element.removeChild(txn, 0); element.removeChild(txn, 0); }- Specified by:
removeChildin interfacenet.carcdr.ycrdt.YXmlElement- Parameters:
txn- Transaction handleindex- The index of the child to remove- Throws:
IllegalArgumentException- if txn is nullIndexOutOfBoundsException- if index is negativeIllegalStateException- if the XML element has been closed
-
getParent
Gets the parent of this XML element. The parent can be either a YXmlElement or YXmlFragment.- Specified by:
getParentin interfacenet.carcdr.ycrdt.YXmlElement- Returns:
- The parent node (YXmlElement or YXmlFragment), or null if this element has no parent
- Throws:
IllegalStateException- if the XML element has been closed
-
getParent
Gets the parent of this XML element using an existing transaction. The parent can be either a YXmlElement or YXmlFragment.- Specified by:
getParentin interfacenet.carcdr.ycrdt.YXmlElement- Parameters:
txn- Transaction handle- Returns:
- The parent node (YXmlElement or YXmlFragment), or null if this element has no parent
- Throws:
IllegalArgumentException- if txn is nullIllegalStateException- if the XML element has been closed
-
getIndexInParent
public int getIndexInParent()Gets the index of this element within its parent's children.- Specified by:
getIndexInParentin interfacenet.carcdr.ycrdt.YXmlElement- Returns:
- The 0-based index within parent, or -1 if this element has no parent
- Throws:
IllegalStateException- if the XML element has been closed
-
getIndexInParent
public int getIndexInParent(net.carcdr.ycrdt.YTransaction txn) Gets the index of this element within its parent's children using an existing transaction.- Specified by:
getIndexInParentin interfacenet.carcdr.ycrdt.YXmlElement- Parameters:
txn- Transaction handle- Returns:
- The 0-based index within parent, or -1 if this element has no parent
- Throws:
IllegalArgumentException- if txn is nullIllegalStateException- if the XML element has been closed
-
observe
public net.carcdr.ycrdt.YSubscription observe(net.carcdr.ycrdt.YObserver observer) Registers an observer to be notified when this XML element changes.The observer will be called whenever children are added/removed or attributes are modified in this element. The observer receives a
YEventcontaining details about the changes.Example:
try (YDoc doc = new JniYDoc(); YXmlElement element = doc.getXmlElement("div"); YSubscription sub = element.observe(event -> { System.out.println("Element changed!"); for (YChange change : event.getChanges()) { // Handle change } })) { element.setAttribute("class", "active"); // Observer is called }- Specified by:
observein interfacenet.carcdr.ycrdt.YXmlElement- Parameters:
observer- The observer to register (must not be null)- Returns:
- A subscription handle that can be used to unregister the observer
- Throws:
IllegalArgumentException- if observer is nullIllegalStateException- if this element has been closed
-
unobserveById
public void unobserveById(long subscriptionId) Unregisters an observer by its subscription ID.This method is typically called automatically when a
YSubscriptionis closed. Users should prefer using try-with-resources with YSubscription rather than calling this method directly.- Parameters:
subscriptionId- The ID of the subscription to remove
-
isClosed
public boolean isClosed()Checks if this YXmlElement has been closed.- Specified by:
isClosedin interfacenet.carcdr.ycrdt.YXmlElement- Returns:
- true if this YXmlElement has been closed, false otherwise
-
close
public void close()Closes this YXmlElement and releases native resources.After calling this method, any operations on this YXmlElement will throw
IllegalStateException.This method is idempotent - calling it multiple times has no effect after the first call.
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfacenet.carcdr.ycrdt.YXmlElement
-