Синтаксический анализ XML с использованием DOM в Java - PullRequest
1 голос
/ 28 июля 2011
<tree>
<declarations>
    <attributeDecl name="name" type="String"/>
</declarations>
<branch>
<attribute name="name" value="Malaria"/>
<branch>
<attribute name="name" value="Excess Stool"/>
<branch>
<attribute name="name" value="Do you have watery stool"/>
<branch>
</branch>
<attribute name="name" value="Do you have this condition for more than a weeks time"/>
<branch>
<attribute name="name" value="No"/>
</branch>
</branch>
<branch>
<attribute name="name" value="No"/>
</branch>
</branch>
<branch>
<attribute name="name" value="High Fever"/>
<branch>
<attribute name="name" value="Was your peak temperature greater than 104"/>
<branch>
<attribute name="name" value="Do you have high fever for more than a weeks time "/>
<branch>
<attribute name="name" value="Do you have chills"/>
</branch>
<branch>
<attribute name="name" value="No"/>
</branch>
</branch>
<branch>
<attribute name="name" value="No"/>
</branch>
</branch>
<branch>
<attribute name="name" value="No"/>
</branch>
</branch>
<branch>
<attribute name="name" value="Bodyache"/>
<branch>
<attribute name="name" value="Do you have pain in the left part of the head "/>
<branch>
<attribute name="name" value="Do you have headache more than 5 times a day "/>
<branch>
<attribute name="name" value="Duration of the headache spans for more than a day "/>
</branch>
<branch>
<attribute name="name" value="No"/>
</branch>
</branch>
<branch>
<attribute name="name" value="No"/>
</branch>
</branch>
<branch>
<attribute name="name" value="No"/>
</branch>
</branch>
</branch>
</tree>

Я должен разобрать этот xml. Я использую синтаксический анализатор DOM для синтаксического анализа одного и того же. Так как в этом случае имя атрибута всегда name, я немного не понимаю, как анализировать xml.

Ответы [ 2 ]

3 голосов
/ 28 июля 2011

Получите помощь из следующего примера

public class DOMParser {
    private Document doc = null;
    public DOMParser() {
        try {
            doc = parserXML(new File("resource/data.xml"));

            visit(doc, 0);
        } catch (Exception error) {
            error.printStackTrace();
        }
    }

    public void visit(Node node, int level) {
        NodeList nl = node.getChildNodes();

        for (int i = 0, cnt = nl.getLength(); i < cnt; i++) {
            if (nl.item(i).hasAttributes()) {
                printAttributes(nl.item(i));
            }

            visit(nl.item(i), level + 1);
        }
    }

    public Document parserXML(File file) throws SAXException, IOException,
            ParserConfigurationException {
        return DocumentBuilderFactory.newInstance().newDocumentBuilder()
                .parse(file);
    }

    public static void main(String[] args) {
        new DOMParser();
    }

    private void printAttributes(Node node) {

        NamedNodeMap attrs = node.getAttributes();
        System.out.print(node.getNodeName());
        for (int i = 0; i < attrs.getLength(); i++) {
            Attr attribute = (Attr) attrs.item(i);
            System.out.print(" : " + attribute.getName() + "="
                    + attribute.getValue());
        }
        System.out.println("");
    }

}
0 голосов
/ 28 июля 2011

см. Как читать XML-файл в Java - (DOM Parser) для анализа XML в Java

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...