【发布时间】:2016-02-21 16:56:24
【问题描述】:
我需要在 Java 中对具有相同属性的 XML 元素进行分组。
以下是我更新后的输入:
<root>
<Slots date="2015-11-17">
<TimePeriod value="8-17">
<ContractorAvailable ContractorID="H4CONT07">
<GroupId>298451</GroupId>
<OfferToken>10315009</OfferToken>
<Capacity>99</Capacity>
</ContractorAvailable>
</TimePeriod>
</Slots>
<Slots date="2015-11-17">
<TimePeriod value="8-17">
<ContractorAvailable ContractorID="H4CONT07">
<GroupId>298452</GroupId>
<OfferToken>10315009</OfferToken>
<Capacity>99</Capacity>
</ContractorAvailable>
</TimePeriod>
</Slots>
<Slots date="2015-11-18">
<TimePeriod value="2-8">
<ContractorAvailable ContractorID="H4CONT07">
<GroupId>298451</GroupId>
<OfferToken>10315009</OfferToken>
<Capacity>99</Capacity>
</ContractorAvailable>
</TimePeriod>
</Slots>
<Slots date="2015-11-18">
<TimePeriod value="2-8">
<ContractorAvailable ContractorID="H4CONT07">
<GroupId>298452</GroupId>
<OfferToken>10315009</OfferToken>
<Capacity>99</Capacity>
</ContractorAvailable>
</TimePeriod>
</Slots>
我需要如下输出:
<root>
<Slots date="2015-11-17">
<TimePeriod value="8-17">
<ContractorAvailable ContractorID="H4CONT07">
<GroupId>298451</GroupId>
<OfferToken>10315009</OfferToken>
<Capacity>99</Capacity>
</ContractorAvailable>
<ContractorAvailable ContractorID="H4CONT07">
<GroupId>298452</GroupId>
<OfferToken>10315009</OfferToken>
<Capacity>99</Capacity>
</ContractorAvailable>
</TimePeriod>
</Slots>
<Slots date="2015-11-18">
<TimePeriod value="2-8">
<ContractorAvailable ContractorID="H4CONT07">
<GroupId>298451</GroupId>
<OfferToken>10315009</OfferToken>
<Capacity>99</Capacity>
</ContractorAvailable>
<ContractorAvailable ContractorID="H4CONT07">
<GroupId>298452</GroupId>
<OfferToken>10315009</OfferToken>
<Capacity>99</Capacity>
</ContractorAvailable>
</TimePeriod>
</Slots>
</root>
我尝试了类似以下的方法:
HashSet<String> set = new HashSet<String>();
if( node!= null && node.getLength() > 0) {
for(int i=0;i<node.getLength();i=i+1)
{
Node slots = node.item(i);
set.add(slots.getAttributes().getNamedItem("date").getNodeValue());
}
}
System.out.println("slots dates are --- **** "+ set);
ArrayList<Node> deleteList = new ArrayList<Node>();
for(String date : set)
{ System.out.println("slots dates is --- **** "+ "2015-11-18");
Node remainingNode = null;
for(int i=0;i<node.getLength();i=i+1)
{
Node slotnode = node.item(i);
if( slotnode.getAttributes() != null && slotnode.getAttributes().getNamedItem("date")!= null && "2015-11-18".equals(slotnode.getAttributes().getNamedItem("date").getNodeValue()))
{System.out.println("inside time 18");
if(remainingNode == null)
remainingNode = slotnode;
else {
NodeList nodeps = slotnode.getChildNodes();
for(int j=0;j<nodeps.getLength();j=i+1) {
remainingNode.appendChild(nodeps.item(j));
}
deleteList.add(slotnode);
}
}
}
NodeList nodesItems = doc1.getElementsByTagName("root");
nodesItems.item(0).appendChild(remainingNode);
我尝试使用 Dom 解析器,但我做不到。我是 XML 新手,到处搜索,但没有找到任何东西,任何人都可以帮我解决这个问题。 注意:我不能将 JAXB 用于此解决方案。
是的,我按日期分组插槽,然后按 TimePeriod 值分组。 我正在使用的工具不支持 XSLT
【问题讨论】:
-
考虑使用 XSLT 2.0 (w3.org/TR/xslt20/#grouping-examples) 或 XQuery 3 (w3.org/TR/xquery-30/#id-group-by) 之类的语言进行分组,这些语言在 Java 上受到 Saxon 9 的支持,开源版本来自saxon.sourceforge.net.
-
您使用哪个 Java 版本?
-
您能否提供一个具有单个根元素的格式良好的 XML 示例?您是否需要将
Slots与date分组,然后再将TimePeriod value分组?或者对于具有相同date的所有Slots,该值是否相同? -
你能使用 Java 8 分组吗?