We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I'm building a fancy XML parser/editor right now, and the syntax of my XML file gives every element a unique ID attribute. I wrote a function that returns a child element if it has an attribute whose value... You know what? Just take a look.
XML getChildAttr(XML Parent, String Attribute, String Value) {
if (Parent.hasChildren()) {
for (XML childN : Parent.getChildren()) {
if (childN.getString(Attribute) != null) {
if (childN.getString(Attribute).equals(Value)) {
println(childN);
return childN;
}
}
}
}
return null;
}
I wish to be able to call it like this: Parent.getChildAttr(Attribute, Value);
, but that would require it to be a method of the XML class. My question is this: Is it possible to add methods to built-in classes? If it is, how would I do so?
Answers
Look at extends?
Do something like this, then? I was really hoping to be able to alter the XML class itself instead of making a new one.
Wait... Do I need a constructor for this?
extends
to established a derivedclass
.class
XML isn't supposed to be directly instantiated.class
instance to be returned. #-ooh gosh. I'm gonna just stick with my boring little function, then. Code readability should never be worth using hackish cheats, which themselves are hard to decipher.