I have an xml file as such:
element
element1
element1
element
Now: element1 can have text, or/and either/both of the two elements element2 element3 so the xml file could be
<element>
<element1>
hi hello etc
</element1>
</element>
or
<element>
<element1>
hi hello etc
<element2>
how are you
</element2>
<element3>
fine
</element3>
</element1>
</element>
I am successfull in parsing it when it is as in the above examples. i can access the nodes by their tags and get the content out of it. problem arises , when the xml is of the following form:
<element>
<element1>
<element2>
how are you
</element2>
</element1>
</element>
I have a loop, that goes through the xml document and checks for the tags element1,element2,element3 and get the contecnt. for the above example i get an acception and the loops terminates. because when it reaches element1, it doesnt get a content,instead there ia another element. please help me solve this problem thanks
If each line only contains element name or text content alone, I will use stack to parse such context. so, define a stack S, and if the XML file as followings:
<element>
<element1>
hi hello etc
<element2>
how are you
</element2>
<element3>
fine
</element3>
</element1>
</element>
So the whole idea may like this:
push <element>
--> S[ <element>
]
<element1>
--> S[ <element>
, <element1>
]
push hi hello etc
--> S[ <element>
, <element1>
, hi hello etc
]
push <element2>
--> S[ <element>
, <element1>
, hi hello etc
, <element2>
]
push how are you
--> S[ <element>
, <element1>
, hi hello etc
, <element2>
, how are you
]
Get </element2>
, then Pop twice, and get how are you
--> S[ <element>
, <element1>
, hi hello etc
]
push <element3>
--> S[ <element>
, <element1>
, hi hello etc
, <element3>
]
push fine
--> S[ <element>
, <element1>
, hi hello etc
, <element3>
, fine
]
Get </element3>
, then Pop twice, and get fine
--> S[ <element>
, <element1>
, hi hello etc
]
Get </element1>
, then Pop twice, and get hi hello etc
--> S[ <element>
]
Is this what you mean??
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.