0

再会!我找不到正常的算法来处理 xml 并为我的实体获取数据。所以我有这样的xml:

    <?xml version="1.0" encoding="UTF-8"?>
<export>
    <categories>
        <category>
            <idgr xmlns:type="string">bd72d90d-55bc-11d9-848a-00112f43529a</idgr>
            <namegr xmlns:type="string">Обувь</namegr>
            <categories>
                <category>
                    <idgr xmlns:type="string">bd72d90e-55bc-11d9-848a-00112f43529a</idgr>
                    <namegr xmlns:type="string">Женская обувь</namegr>
                    <categories>
                        <category>
                            <idgr xmlns:type="string">bd72d910-55bc-11d9-848a-00112f43529a</idgr>
                            <namegr xmlns:type="string">Женские ботфорты коричневые</namegr>
                        </category>
                        <category>
                            <idgr xmlns:type="string">bd72d913-55bc-11d9-848a-00112f43529a</idgr>
                            <namegr xmlns:type="string">Ботинки женские демисезонные</namegr>
                        </category>
                        <category>
                            <idgr xmlns:type="string">bd72d916-55bc-11d9-848a-00112f43529a</idgr>
                            <namegr xmlns:type="string">Ботинки женские натуральная кожа</namegr>
                        </category>
                        <category>
                            <idgr xmlns:type="string">bd72d91b-55bc-11d9-848a-00112f43529a</idgr>
                            <namegr xmlns:type="string">Женские босоножки</namegr>
                        </category>
                        <category>
                            <idgr xmlns:type="string">dee6e178-55bc-11d9-848a-00112f43529a</idgr>
                            <namegr xmlns:type="string">Женские модельные туфли</namegr>
                        </category>
                        <category>
                            <idgr xmlns:type="string">dee6e17b-55bc-11d9-848a-00112f43529a</idgr>
                            <namegr xmlns:type="string">Женские сапоги с искусственным мехом</namegr>
                        </category>
                        <category>
                            <idgr xmlns:type="string">e8a71f9a-55bc-11d9-848a-00112f43529a</idgr>
                            <namegr xmlns:type="string">Женские туфли-мокасины</namegr>
                        </category>
                        <category>
                            <idgr xmlns:type="string">e8a71fa7-55bc-11d9-848a-00112f43529a</idgr>
                            <namegr xmlns:type="string">Женские сапоги с натуральным мехом</namegr>
                        </category>
                        <category>
                            <idgr xmlns:type="string">e8a71fa9-55bc-11d9-848a-00112f43529a</idgr>
                            <namegr xmlns:type="string">Сапоги жен. низкие</namegr>
                        </category>
                        <category>
                            <idgr xmlns:type="string">e8a71faf-55bc-11d9-848a-00112f43529a</idgr>
                            <namegr xmlns:type="string">Сапоги жен высокие</namegr>
                        </category>
                        <category>
                            <idgr xmlns:type="string">e8a71fb3-55bc-11d9-848a-00112f43529a</idgr>
                            <namegr xmlns:type="string">Женские туфли</namegr>
                        </category>
                        <category>
                            <idgr xmlns:type="string">e8a71fb7-55bc-11d9-848a-00112f43529a</idgr>
                            <namegr xmlns:type="string">Полусапожки на шнурках</namegr>
                        </category>
                    </categories>
                </category>
                <category>
                    <idgr xmlns:type="string">dee6e186-55bc-11d9-848a-00112f43529a</idgr>
                    <namegr xmlns:type="string">Мужская обувь</namegr>
                    <categories>
                        <category>
                            <idgr xmlns:type="string">dee6e187-55bc-11d9-848a-00112f43529a</idgr>
                            <namegr xmlns:type="string">Мужские мокасины</namegr>
                        </category>
                        <category>
                            <idgr xmlns:type="string">dee6e18a-55bc-11d9-848a-00112f43529a</idgr>
                            <namegr xmlns:type="string">Мужские ботинки демисезонные</namegr>
                        </category>
                        <category>
                            <idgr xmlns:type="string">e8a71fab-55bc-11d9-848a-00112f43529a</idgr>
                            <namegr xmlns:type="string">Мужские сапоги с натуральным мехом</namegr>
                        </category>
                        <category>
                            <idgr xmlns:type="string">e8a71fad-55bc-11d9-848a-00112f43529a</idgr>
                            <namegr xmlns:type="string">Мужские туфли нубук</namegr>
                        </category>
                        <category>
                            <idgr xmlns:type="string">e8a71fb1-55bc-11d9-848a-00112f43529a</idgr>
                            <namegr xmlns:type="string">Мужские сапоги с искуственным мехом</namegr>
                        </category>
                        <category>
                            <idgr xmlns:type="string">e8a71fb5-55bc-11d9-848a-00112f43529a</idgr>
                            <namegr xmlns:type="string">Ботинки мужские</namegr>
                        </category>
                    </categories>
                </category>
                <category>
                    <idgr xmlns:type="string">dee6e1cf-55bc-11d9-848a-00112f43529a</idgr>
                    <namegr xmlns:type="string">Кроссовки</namegr>
                    <categories>
                        <category>
                            <idgr xmlns:type="string">dee6e1d0-55bc-11d9-848a-00112f43529a</idgr>
                            <namegr xmlns:type="string">Кроссовки "ADIDAS"</namegr>
                        </category>
                        <category>
                            <idgr xmlns:type="string">dee6e1d3-55bc-11d9-848a-00112f43529a</idgr>
                            <namegr xmlns:type="string">Кроссовки "REEBOK"</namegr>
                        </category>
                        <category>
                            <idgr xmlns:type="string">dee6e1d6-55bc-11d9-848a-00112f43529a</idgr>
                            <namegr xmlns:type="string">Кроссовки высокие нат. кожа</namegr>
                        </category>
                        <category>
                            <idgr xmlns:type="string">dee6e1d9-55bc-11d9-848a-00112f43529a</idgr>
                            <namegr xmlns:type="string">Кроссовки мужские, кожа</namegr>
                        </category>
                    </categories>
                </category>
                <category>
                    <idgr xmlns:type="string">05e26d88-01e4-11dc-a411-00055d80a2d1</idgr>
                    <namegr xmlns:type="string">Аксессуары</namegr>
                    <categories>
                        <category>
                            <idgr xmlns:type="string">05e26d89-01e4-11dc-a411-00055d80a2d1</idgr>
                            <namegr xmlns:type="string">Крем для обуви</namegr>
                        </category>
                        <category>
                            <idgr xmlns:type="string">05e26d8b-01e4-11dc-a411-00055d80a2d1</idgr>
                            <namegr xmlns:type="string">Щетка для обуви</namegr>
                        </category>
                    </categories>
                </category>
            </categories>
        </category>
</categories>
</export>

(对不起代码中的俄语文本)我试图解析它

public class ParseXmlData {
    public final static String FILE_PATH = "public/1cimport/export.xml";
    private DocumentBuilderFactory dbfactory;
    private DocumentBuilder documentBuilder;
    private Document doc;

    public final static String CATEGORIES_LABEL = "categories";

    private Integer levelCounter = 0;
    public ParseXmlData(){
        File xmlFile = new File(FILE_PATH);

        dbfactory = DocumentBuilderFactory.newInstance();
        try {
            documentBuilder = dbfactory.newDocumentBuilder();
            doc = documentBuilder.parse(FILE_PATH);
            final List<String> l  = new ArrayList<String>();

            parse(doc, l, doc.getDocumentElement());
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }


    }
    public void parse(final Document doc, final List<String> list, final Element element){

        final NodeList nodeChildrenList = element.getChildNodes();

        for(int i = 0; i<nodeChildrenList.getLength(); i++){
            final Node node = nodeChildrenList.item(i);
            if(node.getNodeType() == Node.ELEMENT_NODE){
               list.add(node.getNodeName());
               if(node.getNodeName().toUpperCase().equals(ParseXmlData.CATEGORIES_LABEL.toUpperCase())){
                   CategoryParseXml categoryParseXml = new CategoryParseXml();
                   categoryParseXml.parseCategory(doc,list,(Element) node);
               }
               parse(doc,list,(Element) node);
            }
        }

    }
}

还有一个班级

public class CategoryParseXml {
    public static Integer levelCounter = 0;
    public final static String IDGR_LABEL = "IDGR";
    public final static String NAMEGR_LABEL = "NAMEGR";

    private List<CategoryEntity> categoryEntities = new ArrayList<CategoryEntity>();

    public void parseCategory(final Document doc, final List<String> list, final Element element){
        final NodeList nodeChildrenList = element.getChildNodes();

        CategoryEntity categoryEntity = new CategoryEntity();
        categoryEntity.setLevel(levelCounter);

        for(int i = 0; i<nodeChildrenList.getLength(); i++){
            final Node node = nodeChildrenList.item(i);
            if(node.getNodeType() == Node.ELEMENT_NODE){
                list.add(node.getNodeName());
                if(node.getNodeName().toUpperCase().equals(ParseXmlData.CATEGORIES_LABEL.toUpperCase())){
                    CategoryParseXml.levelCounter++;
                    parseCategory(doc,list,(Element) node);
                }
                if(node.getNodeName().toUpperCase().equals(CategoryParseXml.IDGR_LABEL.toUpperCase())){
                    categoryEntity.setCategoryId(node.getNodeValue());
                }
                if(node.getNodeName().toUpperCase().equals(CategoryParseXml.NAMEGR_LABEL.toUpperCase())){
                    categoryEntity.setName(node.getNodeValue());
                }

                parseCategory(doc,list,(Element) node);
            }
        }

    }

}

我使用 play framework 和 ebean 作为 ORM 支持。所以实体看起来像

@Entity
public class CategoryEntity extends Model {
    @Id
    private String categoryId;

    private String Name;
@OneToMany(targetEntity = CategoryEntity.class, cascade = CascadeType.ALL)
private List<CategoryEntity> categories;
    private Integer level;
}

我找不到需要获取子类别列表以及需要创建它并添加到类别的时刻。请提供一些想法来从这个 xml 填充这个类实体

4

1 回答 1

0

If I were you I would try a different approach:

  1. Create a XSD schema supporting recursion See this Recursion in an XML schema?
  2. From that xsd schema use JAXB to create classes you can serialize to XML and de-serialize from XML See this too JAXB endless data structure, recursive binding?
  3. I would not recommend it but you can annotate that same JAXB generated classes as JPA entities and use them directly for persistence. An alternative approach would be to create different separated classes for JPA entities and moving values between JPA and JAXB classes using reflection for instance
于 2013-03-23T17:48:31.977 回答