I want to parse the root element of such data as follows
<elementA>
...
...anything
...
</ elementA>
<elementB>
<!--anything such as same element name-->
<elementB>hahahha</elementB>
</elementB>
<
elementC
>
{
”aa“: 11,
}
</
elementC >
If I am successful in parsing, a list will be returned with a structure similar to the following
[
"elementA":"...\n...anything\n...",
"elementB":"<!--anything such as same element name-->\n<elementB>hahahha</elementB>",
"elementC":"{\n\”aa\“: 11,\n}",
]
I also refer to this library dart-xml
Here is my core parsing logic
// 元素:空格 + < 空格 + 名 + 空格 + > + 内容 + </ + 空格 + 名 + 空格 + > + 空格)
Parser element() => ref0(spaceOptional)
.seq(RootToken.openElement.toParser())
.seq(ref0(spaceOptional))
.seq(ref0(wrapName))
.seq(ref0(spaceOptional))
.seq(RootToken.closeElement.toParser())
.seq(ref0(content))
.seq(RootToken.openEndElement.toParser())
.seq(ref0(spaceOptional))
.seq(ref0(wrapName))
.seq(ref0(spaceOptional))
.seq(RootToken.closeElement.toParser())
.seq(ref0(spaceOptional));
But I don't know how to parse the content, because the content contains arbitrary characters, such as the same name as the root element。
Also thanks for this library dart-petitparser and the author, thanks.
Your code looks fine, I assume the question is how to define the content production so that it consumes everything nested into the element?
There are multiple ways depending on your exact requirements. The following code recursively parses other elements OR any other character.
If you want to study a full blown example have a look at the XML parser on GitHub.