From 4198291d9a522e34ce190ad58f524f17f3931e11 Mon Sep 17 00:00:00 2001 From: jules Date: Tue, 1 Oct 2013 13:48:40 +0100 Subject: [PATCH] Added method XmlElement::getChildByAttribute(). --- modules/juce_core/xml/juce_XmlElement.cpp | 13 +++++++++++++ modules/juce_core/xml/juce_XmlElement.h | 12 +++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/modules/juce_core/xml/juce_XmlElement.cpp b/modules/juce_core/xml/juce_XmlElement.cpp index 9ec19c54f9..b721daacf3 100644 --- a/modules/juce_core/xml/juce_XmlElement.cpp +++ b/modules/juce_core/xml/juce_XmlElement.cpp @@ -562,6 +562,8 @@ XmlElement* XmlElement::getChildElement (const int index) const noexcept XmlElement* XmlElement::getChildByName (StringRef childName) const noexcept { + jassert (! childName.isEmpty()); + for (XmlElement* child = firstChildElement; child != nullptr; child = child->nextListItem) if (child->hasTagName (childName)) return child; @@ -569,6 +571,17 @@ XmlElement* XmlElement::getChildByName (StringRef childName) const noexcept return nullptr; } +XmlElement* XmlElement::getChildByAttribute (StringRef attributeName, StringRef attributeValue) const noexcept +{ + jassert (! attributeName.isEmpty()); + + for (XmlElement* child = firstChildElement; child != nullptr; child = child->nextListItem) + if (child->compareAttribute (attributeName, attributeValue)) + return child; + + return nullptr; +} + void XmlElement::addChildElement (XmlElement* const newNode) noexcept { if (newNode != nullptr) diff --git a/modules/juce_core/xml/juce_XmlElement.h b/modules/juce_core/xml/juce_XmlElement.h index 1a00008877..ff47977adf 100644 --- a/modules/juce_core/xml/juce_XmlElement.h +++ b/modules/juce_core/xml/juce_XmlElement.h @@ -483,10 +483,20 @@ public: @param tagNameToLookFor the tag name of the element you want to find @returns the first element with this tag name, or nullptr if none is found - @see getNextElement, isTextElement, getChildElement + @see getNextElement, isTextElement, getChildElement, getChildByAttribute */ XmlElement* getChildByName (StringRef tagNameToLookFor) const noexcept; + /** Returns the first sub-element which has an attribute that matches the given value. + + @param attributeName the name of the attribute to check + @param attributeValue the target value of the attribute + @returns the first element with this attribute value, or nullptr if none is found + @see getChildByName + */ + XmlElement* getChildByAttribute (StringRef attributeName, + StringRef attributeValue) const noexcept; + //============================================================================== /** Appends an element to this element's list of children.