java - 解析 XML 的算法

标签 java xml

我正在阅读其中一篇采访 question关于解析 XML。

我写了一个非常高层次和不完整的算法框架,并正在寻找一些帮助来编写一个简单的算法 [我假设存在一个,因为这是作为面试问题提出的,所以我猜应该可以在 45 分钟内完成].

这是我的尝试:

   // Assume well-formedness
    public static Node parseXML(String xml)
    {

        Node root = new XMLParse().new Node();

        while(!helper.endOfElements())
        {
            // Returns name of root element
            root.name = helper.getName(xml);
            // Returns value of root element
            root.name = helper.getValue(xml);

            // returns each child in a String and returns all such children as 
            // a String Array
            // Basically splits based on <> </> elements and return that as a String
            String[] children = helper.getChildren(xml);

            if(children.length!=0)
            {
                root.childList = new ArrayList<XMLParse.Node>();
                for(int i=0; i<children.length;i++)
                {
                    root.childList.add(parseXML(children[i]));
                }
            }

        }


        return root;
    }


    class Node
    {
        String name;
        String value;
        List<Node> childList;

        public String getName()
        {
            return name;
        }

        public String getValue()
        {
            return value;
        }

        public List<Node> getChildList()
        {
            return childList;
        }

    }


Class helper()
{

// Returns the name of the root of the xml
public static String getName(String XML);

// Returns the value of the root of the xml
public static String getValue(String XML)

// Splits the XML into top level childern of the root of the passed XML
public static String[] getChildren(String XML)

}

我希望有人能给我一个伪代码/执行此操作的代码,或者可能会提供一种在我的算法中实现辅助函数的简单方法。

我知道有内置类可以像 here 中那样执行此操作,但使用它们会达到我猜想的目的。此外,此链接中使用的许多东西都只是接口(interface),所以我找不到 say docBuilder.parse (new File("book.xml")) 方法的任何实现。

最佳答案

关于实际问题

这是一个面试问题的例子,旨在暴露申请人缺乏经验。一个在这个行业没有太多经验的申请人(或者只是在他们的简历上写了 XML 而从未实现过一个项目)可能会急切地离开并开始编写代码。

成功通过几次面试或完成几个使用 XML 的项目的申请人将退后一步并进行一些观察

  • 在面试环境中不可能编写和测试符合标准的解析器。
    • 这需要数周、数月或更长时间。如果问了一个问题但回答时间太长,那么面试官实际上是在问一个不同的问题
    • 如果有足够的经验,您可以看到这些“技巧性问题”并做出相应的回答(询问有关面试官正在寻找的内容的更多信息)
  • 该问题要求提供两种解析 XML 的方法。
    • 这是巧合吗?不是。有两种流行的 XML 处理技术。
    • 问题可能是“描述两种流行的 XML 解析方法并讨论它们的优缺点”是的,这样会更清楚,但面试官也在测试您如何应对需求不明确的情况。

关于解决方案

在面试场景中,我会按照以下方式回答:

I don't think it will be productive to write an actual parser here during the interview. This is because standards compliant parsers are time consuming to write and validate and there are already plenty of implementations for different languages. However, I will describe how to use two popular parsers and how I have used them on past projects. I'll also discuss the pros and cons of each approach.

The first approach that can be used when parsing XML is to treat the entire document as a tree of nodes. This type of parser is called a DOM parser. I used a DOM parser on {insert relevant project experience}. We used a DOM parser because we needed to access different parts of the document at the same time.

The second approach that can be used when parsing XML is to treat the document as a stream of events or nodes. This type of approach is called a SAX parser, I used a SAX parser on {insert relevant project experience}. We used a SAX parser because we couldn't fit the entire document in memory.

{insert discussion on pros and cons}

进一步阅读

http://www.cs.nmsu.edu/~epontell/courses/XML/material/xmlparsers.html

关于java - 解析 XML 的算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6325436/

相关文章:

java - 在滑动选项卡布局中停留在同一选项卡中时切换到不同的 fragment

java - Java 中类似 Satchmo 的项目

java - XML SAX : Explain result in `qName` and `localName` in one example XML file

java - 使用 DocumentBuilderFactory 将文档转换为字符串?

php - Symfony2 命令行 "propel:sql:insert --force"失败

java - 如果不执行位置

java - java 1.4 中的整数+整数

java - JPCAP:尝试提取有效负载数据,但缺少 getTCPData() 方法

java - 如何使用 Eclipse 在 Java 中生成 Web 服务客户端

android - 在 fragment 的微调器中使用 setOnItemSelectedListener 时出现错误?