提问人:Jonas 提问时间:11/22/2022 最后编辑:Jonas 更新时间:11/22/2022 访问量:29
基于有序列表解析xml
parse xml based on ordered List
问:
我有一个有序列表。
order, xml_parent, xml_field
1,Identification,Id
2,Identification.Name,LastName
3,Identification.Name,FirstName
4,Identification.OfficialAddress,StreetName
5,Identification.OfficialAddress,StreetNumber
7,Identification.OfficialAddress,PostalCode
8,Identification.OfficialAddress,City
9,Identification.OfficialAddress,Country
10,Identification.Nationality
13,ReasonIntroduction.AskSomething,FromDate
14,ReasonIntroduction.AskMore,FirstTime
需要解析为
<root>
<Identification>
<Id>1</Id>
<Name>
<LastName>x</LastName>
<FirstName>x</FirstName>
</Name>
<OfficialAddress>
<StreetName>Fifth av.</StreetName>
<StreetNumber>5</StreetNumber>
<PostalCode>3212</PostalCode>
<City>3212</City>
<Country>UK</Country>
</OfficialAddress>
<Nationality>Qatar</Nationality>
</Identification>
<ReasonIntroduction>
<AskSomething>true</AskSomething>
<AskMore>true</AskMore>
</ReasonIntroduction>
</root>
我的文件不是那么大,所以我将使用 java DOM 解析。
我想使用递归来处理这个问题,而不是循环这个有序列表,但我不明白如何获得“下一个”值。
因此,在处理“LastName”时,当处理“Identification.Name”时,我如何获得“下一个”元素“FirstName”,我确实包含在 .
我很确定递归是正确的方法,但我遗漏了一些点。
List<String> parentList = Arrays.asList(parentXmlName.split("\\.", -1));
Element parentElement = document.createElement(field.getXmlParentName());
Element element = document.createElement(field.getXmlName());
element.setTextContent(field.getValue());
parentElement.appendChild(element);
答: 暂无答案
评论
split(".", -1)
- 该方法需要正则表达式。句点是正则表达式中的特殊字符,除非转义。split