diff --git a/library/cpp/xml/document/node-attr.h b/library/cpp/xml/document/node-attr.h index 1378ffdfbff4..6f5445d4c2dc 100644 --- a/library/cpp/xml/document/node-attr.h +++ b/library/cpp/xml/document/node-attr.h @@ -44,6 +44,18 @@ namespace NXml { return t; } + template + TMaybe TNode::TryAttr(TZtStringBuf name) const { + TCharPtr value(xmlGetProp(NodePointer, XMLCHAR(name.c_str()))); + if (!value) { + return Nothing(); + } + + T t; + AttrInternal(value, t, name); + return t; + } + template T TNode::Attr(TZtStringBuf name, const T& defvalue) const { TCharPtr attr(xmlGetProp(NodePointer, XMLCHAR(name.c_str()))); diff --git a/library/cpp/xml/document/xml-document-decl.h b/library/cpp/xml/document/xml-document-decl.h index bfda1fb7e6e3..643ba664680b 100644 --- a/library/cpp/xml/document/xml-document-decl.h +++ b/library/cpp/xml/document/xml-document-decl.h @@ -325,6 +325,14 @@ namespace NXml { template T Attr(TZtStringBuf name) const; + /** + * get node attribute + * @param name: attribute name + * returns value if exists + */ + template + TMaybe TryAttr(TZtStringBuf name) const; + /** * get node attribute * @param name: attribute name @@ -597,6 +605,16 @@ namespace NXml { return ActualNode.NextSibling(); } + /** + * get node attribute + * @param name: attribute name + * returns value if exists + */ + template + TMaybe TryAttr(TZtStringBuf name) const { + return ActualNode.TryAttr(name); + } + /** * get node attribute * @param name: attribute name diff --git a/library/cpp/xml/document/xml-document_ut.cpp b/library/cpp/xml/document/xml-document_ut.cpp index 9f537b75c4c1..0ec1fc608389 100644 --- a/library/cpp/xml/document/xml-document_ut.cpp +++ b/library/cpp/xml/document/xml-document_ut.cpp @@ -1,5 +1,6 @@ #include #include +#include #include "xml-document.h" @@ -35,6 +36,27 @@ Y_UNIT_TEST_SUITE(TestXmlDocument) { NXml::TConstNode text = root.Node("text"); UNIT_ASSERT_EQUAL(text.Value(), "Некоторый текст"); } + Y_UNIT_TEST(GetAttributes) { + NXml::TDocument xml(R"( + + hello world + Некоторый текст + + )", + NXml::TDocument::String); + + NXml::TConstNode root = xml.Root(); + NXml::TConstNode b = root.Node("a/b"); + + UNIT_ASSERT_EXCEPTION_CONTAINS(b.Attr("unknown attrib"), yexception, "@unknown attrib"); + + const auto unknownAttr = b.TryAttr("unknown attrib"); + UNIT_ASSERT(unknownAttr.Empty()); + + const auto knownAttr = b.TryAttr("len"); + UNIT_ASSERT(knownAttr.Defined()); + UNIT_ASSERT_EQUAL(knownAttr.GetRef(), 15); + } Y_UNIT_TEST(SerializeString) { NXml::TDocument xml("frob", NXml::TDocument::RootName); xml.Root().SetAttr("xyzzy", "Frobozz");