【问题标题】:XML parsing having siblings with same name具有同名同级的 XML 解析
【发布时间】:2019-09-18 19:12:43
【问题描述】:

我有一个这样的 XML,必须解析它,需要打印元素是 UI 上的表格格式,或者如果我将获得 JSON,那么我也可以做点什么。

但我无法获取 XML 的所有元素,因为 <type> 标记已被多次使用。

<catalog>
<product productsname="Choclates">
    <Parameters total="2">
        <Subtype name="dairy milk">
            <type>oreo</type>
            <type>Silk</type>
            <type>nuts</type>
        </Subtype>
        <Subtype name="Other">
            <type>perk</type>
            <type>kitkat</type>
            <type>5 start</type>
        </Subtype>
    </Parameters>
</product>
<product productsname="Biscuits">
    <Parameters total="3">
        <Subtype name="parle">
            <type>parle G</type>
            <type>krack jack</type>
            <type>monaco</type>
        </Subtype>
        <Subtype name="britannia">
            <type>good day</type>
            <type>50 50</type>
            <type>bourbon</type>
            <type>tiger</type>
        </Subtype>
        <Subtype name="Priya Gold">
            <type>Italiano Cookies</type>
            <type>Glucose V</type>
            <type>Butter Bite</type>
            <type>CNC</type>
            <type>Marie Lite</type>
            <type>Classic Cream</type>
        </Subtype>
    </Parameters>
</product>

我已经尝试过 DOC 解析器。

【问题讨论】:

  • 这是带有注释的 JAXB 的一个很好的案例,其中一个字段是类型列表。最多两行来加载 XML。
  • 问题是什么?

标签: java json xml


【解决方案1】:

这是一个使用 jdom2 库的示例:

public class Main {

    public static void main(String[] args) {

        org.jdom2.Document jdomDoc;         
        try {   
            jdomDoc = useDOMParser(new File("your_xml_file"));              
            List<Element> products = jdomDoc.getRootElement().getChildren("product");           
            for (Element product : products) {
                System.out.println("----" + product.getAttributeValue("productsname"));
                List<Element> subtypes = product.getChild("Parameters").getChildren("Subtype");
                for (Element subtype : subtypes) {
                    System.out.println("--" + subtype.getAttributeValue("name"));
                    List<Element> types = subtype.getChildren("type");
                    for (Element type : types) {
                        System.out.println(type.getText());
                    }
                }
            }
        } catch (Exception e) {e.printStackTrace();}                
    }

    private static org.jdom2.Document useDOMParser(File fileName)
            throws ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        dbFactory.setIgnoringComments(true);
        DocumentBuilder dBuilder;
        dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fileName);
        DOMBuilder domBuilder = new DOMBuilder();
        return domBuilder.build(doc);

    }   
}

以上产生以下输出:

----Choclates
--dairy milk
oreo
Silk
nuts
--Other
perk
kitkat
5 start
----Biscuits
--parle
parle G
krack jack
monaco
--britannia
good day
50 50
bourbon
tiger
--Priya Gold
Italiano Cookies
Glucose V
Butter Bite
CNC
Marie Lite
Classic Cream

【讨论】:

  • 很难将它转换成一个对象,如果我们在 rest 调用中编写这个逻辑并将 productname 作为参数传递,那么它应该返回该产品的所有详细信息。但我无法将其转换为对象。请帮忙。
  • 只需创建一个具有适当字段/数据结构的产品类,例如包含一个子类型列表,每个子类型包含一个类型列表。然后在解析 XML 时,根据需要创建并填充每个 Product 对象。
猜你喜欢
  • 2012-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多