📄 xmlelement.java
字号:
} return null; } /** * Returns a vector of all child elements named <I>name</I>. * * @param name the full name of the children to search for. * * @return the non-null vector of child elements. */ public Vector getChildrenNamed(String name) { Vector result = new Vector(this.children.size()); Enumeration enum = this.children.elements(); while (enum.hasMoreElements()) { IXMLElement child = (IXMLElement) enum.nextElement(); String childName = child.getFullName(); if ((childName != null) && childName.equals(name)) { result.addElement(child); } } return result; } /** * Returns a vector of all child elements named <I>name</I>. * * @param name the name of the children to search for. * @param namespace the namespace, which may be null. * * @return the non-null vector of child elements. */ public Vector getChildrenNamed(String name, String namespace) { Vector result = new Vector(this.children.size()); Enumeration enum = this.children.elements(); while (enum.hasMoreElements()) { IXMLElement child = (IXMLElement) enum.nextElement(); String str = child.getName(); boolean found = (str != null) && (str.equals(name)); str = child.getNamespace(); if (str == null) { found &= (name == null); } else { found &= str.equals(namespace); } if (found) { result.addElement(child); } } return result; } /** * Searches an attribute. * * @param fullName the non-null full name of the attribute. * * @return the attribute, or null if the attribute does not exist. */ private XMLAttribute findAttribute(String fullName) { Enumeration enum = this.attributes.elements(); while (enum.hasMoreElements()) { XMLAttribute attr = (XMLAttribute) enum.nextElement(); if (attr.getFullName().equals(fullName)) { return attr; } } return null; } /** * Searches an attribute. * * @param name the non-null short name of the attribute. * @param namespace the name space, which may be null. * * @return the attribute, or null if the attribute does not exist. */ private XMLAttribute findAttribute(String name, String namespace) { Enumeration enum = this.attributes.elements(); while (enum.hasMoreElements()) { XMLAttribute attr = (XMLAttribute) enum.nextElement(); boolean found = attr.getName().equals(name); if (namespace == null) { found &= (attr.getNamespace() == null); } else { found &= namespace.equals(attr.getNamespace()); } if (found) { return attr; } } return null; } /** * Returns the number of attributes. */ public int getAttributeCount() { return this.attributes.size(); } /** * @deprecated As of NanoXML/Java 2.1, replaced by * {@link #getAttribute(java.lang.String,java.lang.String)} * Returns the value of an attribute. * * @param name the non-null name of the attribute. * * @return the value, or null if the attribute does not exist. */ public String getAttribute(String name) { return this.getAttribute(name, null); } /** * Returns the value of an attribute. * * @param name the non-null full name of the attribute. * @param defaultValue the default value of the attribute. * * @return the value, or defaultValue if the attribute does not exist. */ public String getAttribute(String name, String defaultValue) { XMLAttribute attr = this.findAttribute(name); if (attr == null) { return defaultValue; } else { return attr.getValue(); } } /** * Returns the value of an attribute. * * @param name the non-null name of the attribute. * @param namespace the namespace URI, which may be null. * @param defaultValue the default value of the attribute. * * @return the value, or defaultValue if the attribute does not exist. */ public String getAttribute(String name, String namespace, String defaultValue) { XMLAttribute attr = this.findAttribute(name, namespace); if (attr == null) { return defaultValue; } else { return attr.getValue(); } } /** * Returns the value of an attribute. * * @param name the non-null full name of the attribute. * @param defaultValue the default value of the attribute. * * @return the value, or defaultValue if the attribute does not exist. */ public int getAttribute(String name, int defaultValue) { String value = this.getAttribute(name, Integer.toString(defaultValue)); return Integer.parseInt(value); } /** * Returns the value of an attribute. * * @param name the non-null name of the attribute. * @param namespace the namespace URI, which may be null. * @param defaultValue the default value of the attribute. * * @return the value, or defaultValue if the attribute does not exist. */ public int getAttribute(String name, String namespace, int defaultValue) { String value = this.getAttribute(name, namespace, Integer.toString(defaultValue)); return Integer.parseInt(value); } /** * Returns the type of an attribute. * * @param name the non-null full name of the attribute. * * @return the type, or null if the attribute does not exist. */ public String getAttributeType(String name) { XMLAttribute attr = this.findAttribute(name); if (attr == null) { return null; } else { return attr.getType(); } } /** * Returns the namespace of an attribute. * * @param name the non-null full name of the attribute. * * @return the namespace, or null if there is none associated. */ public String getAttributeNamespace(String name) { XMLAttribute attr = this.findAttribute(name); if (attr == null) { return null; } else { return attr.getNamespace(); } } /** * Returns the type of an attribute. * * @param name the non-null name of the attribute. * @param namespace the namespace URI, which may be null. * * @return the type, or null if the attribute does not exist. */ public String getAttributeType(String name, String namespace) { XMLAttribute attr = this.findAttribute(name, namespace); if (attr == null) { return null; } else { return attr.getType(); } } /** * Sets an attribute. * * @param name the non-null full name of the attribute. * @param value the non-null value of the attribute. */ public void setAttribute(String name, String value) { XMLAttribute attr = this.findAttribute(name); if (attr == null) { attr = new XMLAttribute(name, name, null, value, "CDATA"); this.attributes.addElement(attr); } else { attr.setValue(value); } } /** * Sets an attribute. * * @param fullName the non-null full name of the attribute. * @param namespace the namespace URI of the attribute, which may be null. * @param value the non-null value of the attribute. */ public void setAttribute(String fullName, String namespace, String value) { int index = fullName.indexOf(':'); String name = fullName.substring(index + 1); XMLAttribute attr = this.findAttribute(name, namespace); if (attr == null) { attr = new XMLAttribute(fullName, name, namespace, value, "CDATA"); this.attributes.addElement(attr); } else { attr.setValue(value); } } /** * Removes an attribute. * * @param name the non-null name of the attribute. */ public void removeAttribute(String name) { for (int i = 0; i < this.attributes.size(); i++) { XMLAttribute attr = (XMLAttribute) this.attributes.elementAt(i); if (attr.getFullName().equals(name)) { this.attributes.removeElementAt(i); return; } } } /** * Removes an attribute. * * @param name the non-null name of the attribute. * @param namespace the namespace URI of the attribute, which may be null. */ public void removeAttribute(String name, String namespace) { for (int i = 0; i < this.attributes.size(); i++) { XMLAttribute attr = (XMLAttribute) this.attributes.elementAt(i); boolean found = attr.getName().equals(name); if (namespace == null) { found &= (attr.getNamespace() == null); } else { found &= attr.getNamespace().equals(namespace); } if (found) { this.attributes.removeElementAt(i); return; } } } /** * Returns an enumeration of all attribute names. * * @return the non-null enumeration. */ public Enumeration enumerateAttributeNames() { Vector result = new Vector(); Enumeration enum = this.attributes.elements(); while (enum.hasMoreElements()) { XMLAttribute attr = (XMLAttribute) enum.nextElement(); result.addElement(attr.getFullName()); } return result.elements(); } /** * Returns whether an attribute exists. * * @return true if the attribute exists. */ public boolean hasAttribute(String name) { return this.findAttribute(name) != null; } /** * Returns whether an attribute exists. * * @return true if the attribute exists. */ public boolean hasAttribute(String name, String namespace) { return this.findAttribute(name, namespace) != null; } /** * Returns all attributes as a Properties object. * * @return the non-null set. */ public Properties getAttributes() { Properties result = new Properties(); Enumeration enum = this.attributes.elements(); while (enum.hasMoreElements()) { XMLAttribute attr = (XMLAttribute) enum.nextElement(); result.put(attr.getFullName(), attr.getValue()); } return result; } /** * Returns all attributes in a specific namespace as a Properties object. * * @param namespace the namespace URI of the attributes, which may be null. * * @return the non-null set. */ public Properties getAttributesInNamespace(String namespace) { Properties result = new Properties(); Enumeration enum = this.attributes.elements(); while (enum.hasMoreElements()) { XMLAttribute attr = (XMLAttribute) enum.nextElement(); if (namespace == null) { if (attr.getNamespace() == null) { result.put(attr.getName(), attr.getValue()); } } else { if (namespace.equals(attr.getNamespace())) { result.put(attr.getName(), attr.getValue()); } } } return result; } /** * Returns the system ID of the data where the element started. * * @return the system ID, or null if unknown. * * @see #getLineNr */ public String getSystemID() { return this.systemID; } /** * Returns the line number in the data where the element started. * * @return the line number, or NO_LINE if unknown. * * @see #NO_LINE * @see #getSystemID */ public int getLineNr() { return this.lineNr; } /** * Return the #PCDATA content of the element. If the element has a * combination of #PCDATA content and child elements, the #PCDATA * sections can be retrieved as unnamed child objects. In this case, * this method returns null. * * @return the content. */ public String getContent() { return this.content; } /** * Sets the #PCDATA content. It is an error to call this method with a * non-null value if there are child objects. * * @param content the (possibly null) content. */ public void setContent(String content) { this.content = content; } /** * Returns true if the element equals another element. * * @param rawElement the element to compare to */ public boolean equals(Object rawElement) { try { return this.equalsXMLElement((IXMLElement) rawElement); } catch (ClassCastException e) { return false; } } /** * Returns true if the element equals another element. * * @param rawElement the element to compare to */ public boolean equalsXMLElement(IXMLElement elt) { if (! this.name.equals(elt.getName())) { return false; } if (this.attributes.size() != elt.getAttributeCount()) { return false; } Enumeration enum = this.attributes.elements(); while (enum.hasMoreElements()) { XMLAttribute attr = (XMLAttribute) enum.nextElement(); if (! elt.hasAttribute(attr.getName(), attr.getNamespace())) { return false; } String value = elt.getAttribute(attr.getName(), attr.getNamespace(), null); if (! attr.getValue().equals(value)) { return false; } String type = elt.getAttributeType(attr.getName(), attr.getNamespace()); if (! attr.getType().equals(type)) { return false; } } if (this.children.size() != elt.getChildrenCount()) { return false; } for (int i = 0; i < this.children.size(); i++) { IXMLElement child1 = this.getChildAtIndex(i); IXMLElement child2 = elt.getChildAtIndex(i); if (! child1.equalsXMLElement(child2)) { return false; } } return true; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -