开发者

XML parsing using DOM in java

<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"开发者_如何学C 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>

I have to parse this xml .I am using DOM parser to parse the same .Since attribute name is always name in this case , i am bit clueless as in how to parse the xml .


Take help from the following example

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("");
    }

}


see How to read XML file in Java – (DOM Parser) for xml parsing in java

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜