如何根据名称的字母顺序对以下xml进行排序,并将其转换为Java中的JSON? 华夫饼 $5.95 我们的两个著名的比利时华夫饼干,里面有很多真正的枫糖浆 650 草莓比利时华夫饼干 $7.95 覆盖草莓和鲜奶油的淡比利时华夫饼干 900 浆果比利时华夫饼干 $8.95 淡比利时华夫饼干,上面覆盖着各种新鲜浆果和鲜奶油 900 法式吐司 $4.50 用我们自制的酸面包做成的厚片 600
在按字母顺序对xml进行排序并将其转换为JSON数组时,我被绊倒了。如何根据名称的字母顺序对以下xml进行排序,并将其转换为Java中的JSON? 华夫饼 $5.95 我们的两个著名的比利时华夫饼干,里面有很多真正的枫糖浆 650 草莓比利时华夫饼干 $7.95 覆盖草莓和鲜奶油的淡比利时华夫饼干 900 浆果比利时华夫饼干 $8.95 淡比利时华夫饼干,上面覆盖着各种新鲜浆果和鲜奶油 900 法式吐司 $4.50 用我们自制的酸面包做成的厚片 600,java,json,xml,Java,Json,Xml,在按字母顺序对xml进行排序并将其转换为JSON数组时,我被绊倒了。 早餐菜单需要按名称排序如果您使用的是Java8,您应该查看我的开源库:。unXml基本上从XPath映射到Json属性 可以在网上买到 范例 按字母顺序是什么键?名称标记内容?什么环境?您是如何处理XML的?DOM还是stream?是否涉及XSL?您几乎没有提供任何上下文,因此不可能弄清楚您想要做什么。此外,你还需要展示你已经尝试了什么,以及为什么你的努力没有奏效。请访问。嗨,吉姆,我在下面提到,我需要根据标签对其进行排序。我
早餐菜单需要按名称排序如果您使用的是Java8,您应该查看我的开源库:。unXml基本上从XPath映射到Json属性 可以在网上买到 范例
按字母顺序是什么键?
名称
标记内容?什么环境?您是如何处理XML的?DOM还是stream?是否涉及XSL?您几乎没有提供任何上下文,因此不可能弄清楚您想要做什么。此外,你还需要展示你已经尝试了什么,以及为什么你的努力没有奏效。请访问。嗨,吉姆,我在下面提到,我需要根据标签对其进行排序。我需要在Java中使用DOM或SAX解析器
<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>
Two of our famous Belgian Waffles with plenty of real maple syrup
</description>
<calories>650</calories>
</food>
<food>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<description>
Light Belgian waffles covered with strawberries and whipped cream
</description>
<calories>900</calories>
</food>
<food>
<name>Berry-Berry Belgian Waffles</name>
<price>$8.95</price>
<description>
Light Belgian waffles covered with an assortment of fresh berries and whipped cream
</description>
<calories>900</calories>
</food>
<food>
<name>French Toast</name>
<price>$4.50</price>
<description>
Thick slices made from our homemade sourdough bread
</description>
<calories>600</calories>
</food>
</breakfast_menu>
import java.util.stream.StreamSupport;
import java.util.stream.Collectors;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.nerdforge.unxml.factory.ParsingFactory;
import com.nerdforge.unxml.parsers.Parser;
import org.w3c.dom.Document;
public class Parser {
public List<JsonNode> parseXml(String xml){
Document document = parsing.xml().document(xml);
// parsing xml to Json
Parser<ArrayNode> parser = parsing.arr("/breakfast_menu/food")
.attribute("name")
.attribute("price")
.attribute("description")
.attribute("calories")
.build();
ArrayNode jsonArray = parser.apply(document);
// sorting
List<JsonNode> result = StreamSupport.stream(jsonArray.spliterator(), false)
.sorted((first, second) -> first.get("name").asText().compareTo(second.get("name").asText()))
.collect(Collectors.toList());
return result;
}
}
[{
"price" : "$5.95",
"name" : "Belgian Waffles",
"description" : " Two of our famous Belgian Waffles with plenty of real maple syrup ",
"calories" : "650"
},
{
"price" : "$8.95",
"name" : "Berry-Berry Belgian Waffles",
"description" : " Light Belgian waffles covered with an assortment of fresh berries and whipped cream ",
"calories" : "900"
},
{
"price" : "$4.50",
"name" : "French Toast",
"description" : " Thick slices made from our homemade sourdough bread ",
"calories" : "600"
},
{
"price" : "$7.95",
"name" : "Strawberry Belgian Waffles",
"description" : " Light Belgian waffles covered with strawberries and whipped cream ",
"calories" : "900"
}]