Java - 使用 DOM 解析 xml

发布于 2024-12-19 07:35:44 字数 1790 浏览 0 评论 0原文

我正在尝试解析以下 xml。我可以轻松访问 WeekNumber,但无法访问 EmployeeRatesLevelA 和 EmployeeRatesLevelB 的子级。目标是将它们保存到一个类、带有字段 WeekNumber 和 ArrayLists、EmployeeRatesLevelA 和 EmployeeRatesLevelB 的 DataSet。 谢谢。

<DataSet ActiveFrom="2011/04/06">  
    <WeekNumber>8</WeekNumber>  
    <EmployeeRatesLevelA>  
        <Rate>0</Rate>  
        <Rate>0.12</Rate>  
    </EmployeeRatesLevelA>  
    <EmployeeRatesLevelB>  
        <Rate>0.15</Rate>  
        <Rate>0.20</Rate>  
    </EmployeeRatesLevelB>  
</DataSet>  


    Document doc = loadXml("data.xml");  
    NodeList nodeList = doc.getElementsByTagName("DataSet");  
    for (int i = 0; i < nodeList.getLength(); i++) {  
        Node node = nodeList.item(i);  
        if (node.getNodeType() == Node.ELEMENT_NODE) {  
            Element element = (Element) node;  
            NodeList weekNumberList = element.getElementsByTagName("WeekNumber");  
            Element weekElement = (Element) weekNumberList.item(0);  
            NodeList textElementList = weekElement.getChildNodes();
            System.out.println("Weeknumber:"+ ((Node)textElementList.item(0)).getNodeValue().trim());
    }

    public static Document loadXml(String file) {
        try {
           return (DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(file)));
        } catch (SAXException e) {
        e.printStackTrace();
        } catch (IOException e) {
        e.printStackTrace();
        } catch (ParserConfigurationException e) {
        e.printStackTrace();
        }
        return null;
    }

这给了我周数,但我无法访问 EmployeeRatesLevelA 和 EmployeeRatesLevelB。

想要学习其他很酷的东西,但由于我是 Java 新手,而且 xml 文档非常小,DOM 应该足够了。

I am trying to parse the following xml. I can access the WeekNumber easily but cannot access the children for EmployeeRatesLevelA and EmployeeRatesLevelB. The goal is to save these to a class, DataSet with fields WeekNumber and ArrayLists, EmployeeRatesLevelA and EmployeeRatesLevelB.
Thanks.

<DataSet ActiveFrom="2011/04/06">  
    <WeekNumber>8</WeekNumber>  
    <EmployeeRatesLevelA>  
        <Rate>0</Rate>  
        <Rate>0.12</Rate>  
    </EmployeeRatesLevelA>  
    <EmployeeRatesLevelB>  
        <Rate>0.15</Rate>  
        <Rate>0.20</Rate>  
    </EmployeeRatesLevelB>  
</DataSet>  


    Document doc = loadXml("data.xml");  
    NodeList nodeList = doc.getElementsByTagName("DataSet");  
    for (int i = 0; i < nodeList.getLength(); i++) {  
        Node node = nodeList.item(i);  
        if (node.getNodeType() == Node.ELEMENT_NODE) {  
            Element element = (Element) node;  
            NodeList weekNumberList = element.getElementsByTagName("WeekNumber");  
            Element weekElement = (Element) weekNumberList.item(0);  
            NodeList textElementList = weekElement.getChildNodes();
            System.out.println("Weeknumber:"+ ((Node)textElementList.item(0)).getNodeValue().trim());
    }

    public static Document loadXml(String file) {
        try {
           return (DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(file)));
        } catch (SAXException e) {
        e.printStackTrace();
        } catch (IOException e) {
        e.printStackTrace();
        } catch (ParserConfigurationException e) {
        e.printStackTrace();
        }
        return null;
    }

This gives me the Weeknumber but I am unable to access EmployeeRatesLevelA and EmployeeRatesLevelB.

Would like to learn other cool stuff but as I am new to Java and the xml document is really small, DOM should suffice.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

森末i 2024-12-26 07:35:44

正如评论中提到的,请向我们展示您的 Java 代码。您可能还需要考虑查看 JAXB——XML 绑定的 Java 架构。这专门用于将 XML 表示为 Java 对象。无论出于何种原因,它可能对您的解决方案都不可行,但一定要看看:

http://jaxb.java。 net/tutorial/

DataSet

下面的域对象是您在问题中描述的内容:

目标是将这些保存到一个带有字段 WeekNumber 的类 DataSet 中
以及 ArrayLists、EmployeeRatesLevelA 和 EmployeeRatesLevelB。

package forum8345529;

import java.util.List;
import javax.xml.bind.annotation.*;

@XmlRootElement(name="DataSet")
@XmlAccessorType(XmlAccessType.FIELD)
public class DataSet {

    @XmlElement(name="WeekNumber")
    private int weekNumber;

    @XmlElementWrapper(name="EmployeeRatesLevelA")
    @XmlElement(name="Rate")
    private List<Float> employeeRatesLevelA;

    @XmlElementWrapper(name="EmployeeRatesLevelB")
    @XmlElement(name="Rate")
    private List<Float> employeeRatesLevelB;

}

演示

以下代码演示了如何使用 JAXB 运行时将 XML 与域对象相互转换:

package forum8345529;

import java.io.File;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception{
        JAXBContext jc = JAXBContext.newInstance(DataSet.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum8345529/input.xml");
        DataSet dataSet = (DataSet) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(dataSet, System.out);
    }

}

As mentioned in the comments, please show us your Java code. You may also want to consider looking at JAXB - the Java Architecture for XML Binding. This is specifically geared towards representing XML as Java objects. It may not be feasible for your solution for whatever reason, but definitely take a look:

http://jaxb.java.net/tutorial/

DataSet

The following domain object below is what you described in your question:

The goal is to save these to a class, DataSet with fields WeekNumber
and ArrayLists, EmployeeRatesLevelA and EmployeeRatesLevelB.

package forum8345529;

import java.util.List;
import javax.xml.bind.annotation.*;

@XmlRootElement(name="DataSet")
@XmlAccessorType(XmlAccessType.FIELD)
public class DataSet {

    @XmlElement(name="WeekNumber")
    private int weekNumber;

    @XmlElementWrapper(name="EmployeeRatesLevelA")
    @XmlElement(name="Rate")
    private List<Float> employeeRatesLevelA;

    @XmlElementWrapper(name="EmployeeRatesLevelB")
    @XmlElement(name="Rate")
    private List<Float> employeeRatesLevelB;

}

Demo

The following code demonstrates how to use the JAXB runtime to convert your XML to/from your domain objects:

package forum8345529;

import java.io.File;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception{
        JAXBContext jc = JAXBContext.newInstance(DataSet.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum8345529/input.xml");
        DataSet dataSet = (DataSet) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(dataSet, System.out);
    }

}
紫瑟鸿黎 2024-12-26 07:35:44

如果您想使用 DOM,我建议您首先编写一些帮助器类,以使您的工作更轻松。这是我最近写的一篇供我个人使用的文章。

让我们从包中的帮助器类开始 xml.utils

XmlException.java

package xml.utils;

public class XmlException extends Exception {
    private static final long serialVersionUID = 1L;

    public XmlException(String message, Throwable cause)  {
        super(message, cause);
    }

    public XmlException(String message)  {
        super(message);
    }

    public XmlException(Throwable cause)  {
        super(cause);
    }
}

XmlDocument.java

package xml.utils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

public class XmlDocument {
    private Document document;

    public XmlNode parse(InputStream is) throws XmlException {
        try {
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            document = dBuilder.parse(is);
            document.getDocumentElement().normalize();

            XmlNode node = new XmlNode(document.getDocumentElement());
            return node;
        } catch (ParserConfigurationException e) {
            throw new XmlException("Error in configuration of XML parser", e);
        } catch (SAXException e) {
            throw new XmlException("Error in parsing XML document", e);
        } catch (IOException e) {
            throw new XmlException("Error in reading InputStream", e);
        }
    }

    public XmlNode parse(String uri) throws XmlException {
        try {
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            document = dBuilder.parse(uri);
            document.getDocumentElement().normalize();

            XmlNode node = new XmlNode(document.getDocumentElement());
            return node;
        } catch (ParserConfigurationException e) {
            throw new XmlException("Error in configuration of XML parser", e);
        } catch (SAXException e) {
            throw new XmlException("Error in parsing XML document", e);
        } catch (IOException e) {
            throw new XmlException("Error in opening URI", e);
        }
    }

    public XmlNode parse(File file) throws XmlException {
        try {
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            document = dBuilder.parse(file);
            document.getDocumentElement().normalize();

            XmlNode node = new XmlNode(document.getDocumentElement());
            return node;
        } catch (ParserConfigurationException e) {
            throw new XmlException("Error in configuration of XML parser", e);
        } catch (SAXException e) {
            throw new XmlException("Error in parsing XML document", e);
        } catch (IOException e) {
            throw new XmlException("Error in opening file", e);
        }
    }

    public void write(OutputStream os, XmlNode node) throws XmlException {
        try {
            if (document == null) {
                document = createNewDocument();
            }
            document.appendChild(node.getNode());

            // write the content into xml file
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(document);
            StreamResult result = new StreamResult(os);

            transformer.transform(source, result);
        } catch (TransformerConfigurationException e) {
            throw new XmlException("Error in configuration of XML writer", e);
        } catch (TransformerException e) {
            throw new XmlException("Error in writing XML", e);
        }
    }

    public void write(File file, XmlNode node) throws XmlException {
        try {
            if (document == null) {
                document = createNewDocument();
            }
            document.appendChild(node.getNode());

            // write the content into xml file
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(document);
            StreamResult result = new StreamResult(file);

            transformer.transform(source, result);
        } catch (TransformerConfigurationException e) {
            throw new XmlException("Error in configuration of XML writer", e);
        } catch (TransformerException e) {
            throw new XmlException("Error in writing XML", e);
        }
    }



    public void write(Writer writer, XmlNode node) throws XmlException {
        try {
            if (document == null) {
                document = createNewDocument();
            }
            document.appendChild(node.getNode());

            // write the content into xml file
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(document);
            StreamResult result = new StreamResult(writer);

            transformer.transform(source, result);
        } catch (TransformerConfigurationException e) {
            throw new XmlException("Error in configuration of XML writer", e);
        } catch (TransformerException e) {
            throw new XmlException("Error in writing XML", e);
        }
    }

    private Document createNewDocument() throws XmlException {
        try {
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            return dBuilder.newDocument();
        } catch (ParserConfigurationException e) {
            throw new XmlException("Error in configuration of XML parser", e);
        }
    }

    public XmlNode createNode(String nodeName) throws XmlException {
        if (document == null) {
            document = createNewDocument();
        }
        XmlNode node = new XmlNode(this, document.createElement(nodeName));
        return node;
    }

    XmlNode createNode(String nodeName, String nodeValue) throws XmlException {
        if (document == null) {
            document = createNewDocument();
        }
        Element node = document.createElement(nodeName);
        node.appendChild(document.createTextNode(nodeValue));

        return new XmlNode(this, node);
    }
}

XmlNode.java

package xml.utils;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XmlNode {
    private Element node;
    private XmlDocument parent;

    XmlNode(Element node) {
        this.node = node;
        this.parent = null;
    }

    XmlNode(XmlDocument parent, Element node) {
        this.node = node;
        this.parent = parent;
    }

    Node getNode() {
        return node;
    }

        public String getNodeValue() {
            return node.getTextContent();
        }

    public XmlDocument getParent() {
        return parent;
    }

    public void setParent(XmlDocument parent) {
        this.parent = parent;
    }

    public List<XmlNode> getChildNodes() {
        List<XmlNode> list = new ArrayList<XmlNode>();
        NodeList nodeList = node.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node n = nodeList.item(i);
            if (n.getNodeType() == Node.ELEMENT_NODE) {
                list.add(new XmlNode((Element) n));
            }
        }

        return list;
    }

    public XmlNode getFirstChild() {
        return getChildNodes().get(0);
    }

    public XmlNode getLastChild() {
        List<XmlNode> childs = getChildNodes();
        if (childs.size() == 0)
            return null;

        return childs.get(childs.size() - 1);
    }

    public List<XmlNode> getNodesByTagName(String tagName) {
        List<XmlNode> list = new ArrayList<XmlNode>();
        NodeList nodeList = node.getElementsByTagName(tagName);
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node n = nodeList.item(i);
            if (n.getNodeType() == Node.ELEMENT_NODE) {
                list.add(new XmlNode((Element) n));
            }
        }

        return list;
    }

    public XmlNode getFirstNodeByTagName(String tagName) {
        return getNodesByTagName(tagName).get(0);
    }

    public String getTagValue(String tagName) throws XmlException {
        NodeList tagList = node.getElementsByTagName(tagName);
        if (tagList.getLength() == 0)
            throw new XmlException("Tag: '" + tagName + "' not present");

        NodeList nlList = tagList.item(0).getChildNodes();       
        Node nValue = (Node) nlList.item(0);

        return nValue.getNodeValue();
    }

    public String getAttributeValue(String attributeName) {
        return node.getAttribute(attributeName);
    }

    public String getNodeName() {
        return node.getTagName();
    }

    public void setAttribute(String name, String value) throws XmlException {
        if (parent == null) 
            throw new XmlException("Parent node not present.");

        node.setAttribute(name, value);
    }

    public void setTag(String name, String value) throws XmlException {
        if (parent == null) 
            throw new XmlException("Parent node not present.");

        XmlNode xmlNode = parent.createNode(name, value);
        node.appendChild(xmlNode.node);
    }

    public void addChildNode(XmlNode xmlNode) throws XmlException {
        if (parent == null) 
            throw new XmlException("Parent node not present.");

        node.appendChild(xmlNode.node);
    }

    public XmlNode addChildNode(String nodeName) throws XmlException {
        if (parent == null) 
            throw new XmlException("Parent node not present.");

        XmlNode child = parent.createNode(nodeName);
        node.appendChild(child.getNode());

        return child;
    }
}

现在DataSet.java和Main.java如下:

DataSet.java

package tests;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import xml.utils.XmlDocument;
import xml.utils.XmlException;
import xml.utils.XmlNode;

public class DataSet {
    private int weekNumber;
    private List<Float> employeeRatesLevelA;
    private List<Float> employeeRatesLevelB;

    public DataSet(File xml) throws XmlException {
        employeeRatesLevelA = new ArrayList<Float>();
        employeeRatesLevelB = new ArrayList<Float>();

        loadFromXml(xml);
    }

    private void loadFromXml(File xml) throws XmlException {
        XmlDocument document = new XmlDocument();
        XmlNode root = document.parse(xml);

        weekNumber = Integer.parseInt(root.getTagValue("WeekNumber"));

        XmlNode ratesLevelNode = root.getNodesByTagName("EmployeeRatesLevelA").get(0);
        List<XmlNode> rates = ratesLevelNode.getNodesByTagName("Rate");
        for (XmlNode xmlNode : rates) {
            employeeRatesLevelA.add(Float.parseFloat(xmlNode.getNodeValue()));
        }

        ratesLevelNode = root.getNodesByTagName("EmployeeRatesLevelB").get(0);
        rates = ratesLevelNode.getNodesByTagName("Rate");
        for (XmlNode xmlNode : rates) {
            employeeRatesLevelB.add(Float.parseFloat(xmlNode.getNodeValue()));
        }
    }

    public void display() {
        System.out.println("WeekNumber: " + weekNumber);
        System.out.println("Level A");
        for (Float rate : employeeRatesLevelA) {
            System.out.println("\tRate: " + rate);
        }

        System.out.println("Level B");
        for (Float rate : employeeRatesLevelB) {
            System.out.println("\tRate: " + rate);
        }
    }
}

Main.java

package tests;

import java.io.File;
import java.io.IOException;
import org.xml.sax.SAXException;
import xml.utils.XmlException;

public class Main {
    public static void main(String[] args) throws SAXException, IOException, XmlException {
        File dataFile = new File("/home/jomit/data.xml");
        DataSet dataSet = new DataSet(dataFile);
        dataSet.display();
    }
}

If you want to use DOM, I suggest you to start by writing some helper classes to make your work easier. Here is one I written recently for my personal use.

Let's start with the helper classes in package xml.utils

XmlException.java

package xml.utils;

public class XmlException extends Exception {
    private static final long serialVersionUID = 1L;

    public XmlException(String message, Throwable cause)  {
        super(message, cause);
    }

    public XmlException(String message)  {
        super(message);
    }

    public XmlException(Throwable cause)  {
        super(cause);
    }
}

XmlDocument.java

package xml.utils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

public class XmlDocument {
    private Document document;

    public XmlNode parse(InputStream is) throws XmlException {
        try {
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            document = dBuilder.parse(is);
            document.getDocumentElement().normalize();

            XmlNode node = new XmlNode(document.getDocumentElement());
            return node;
        } catch (ParserConfigurationException e) {
            throw new XmlException("Error in configuration of XML parser", e);
        } catch (SAXException e) {
            throw new XmlException("Error in parsing XML document", e);
        } catch (IOException e) {
            throw new XmlException("Error in reading InputStream", e);
        }
    }

    public XmlNode parse(String uri) throws XmlException {
        try {
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            document = dBuilder.parse(uri);
            document.getDocumentElement().normalize();

            XmlNode node = new XmlNode(document.getDocumentElement());
            return node;
        } catch (ParserConfigurationException e) {
            throw new XmlException("Error in configuration of XML parser", e);
        } catch (SAXException e) {
            throw new XmlException("Error in parsing XML document", e);
        } catch (IOException e) {
            throw new XmlException("Error in opening URI", e);
        }
    }

    public XmlNode parse(File file) throws XmlException {
        try {
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            document = dBuilder.parse(file);
            document.getDocumentElement().normalize();

            XmlNode node = new XmlNode(document.getDocumentElement());
            return node;
        } catch (ParserConfigurationException e) {
            throw new XmlException("Error in configuration of XML parser", e);
        } catch (SAXException e) {
            throw new XmlException("Error in parsing XML document", e);
        } catch (IOException e) {
            throw new XmlException("Error in opening file", e);
        }
    }

    public void write(OutputStream os, XmlNode node) throws XmlException {
        try {
            if (document == null) {
                document = createNewDocument();
            }
            document.appendChild(node.getNode());

            // write the content into xml file
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(document);
            StreamResult result = new StreamResult(os);

            transformer.transform(source, result);
        } catch (TransformerConfigurationException e) {
            throw new XmlException("Error in configuration of XML writer", e);
        } catch (TransformerException e) {
            throw new XmlException("Error in writing XML", e);
        }
    }

    public void write(File file, XmlNode node) throws XmlException {
        try {
            if (document == null) {
                document = createNewDocument();
            }
            document.appendChild(node.getNode());

            // write the content into xml file
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(document);
            StreamResult result = new StreamResult(file);

            transformer.transform(source, result);
        } catch (TransformerConfigurationException e) {
            throw new XmlException("Error in configuration of XML writer", e);
        } catch (TransformerException e) {
            throw new XmlException("Error in writing XML", e);
        }
    }



    public void write(Writer writer, XmlNode node) throws XmlException {
        try {
            if (document == null) {
                document = createNewDocument();
            }
            document.appendChild(node.getNode());

            // write the content into xml file
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(document);
            StreamResult result = new StreamResult(writer);

            transformer.transform(source, result);
        } catch (TransformerConfigurationException e) {
            throw new XmlException("Error in configuration of XML writer", e);
        } catch (TransformerException e) {
            throw new XmlException("Error in writing XML", e);
        }
    }

    private Document createNewDocument() throws XmlException {
        try {
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            return dBuilder.newDocument();
        } catch (ParserConfigurationException e) {
            throw new XmlException("Error in configuration of XML parser", e);
        }
    }

    public XmlNode createNode(String nodeName) throws XmlException {
        if (document == null) {
            document = createNewDocument();
        }
        XmlNode node = new XmlNode(this, document.createElement(nodeName));
        return node;
    }

    XmlNode createNode(String nodeName, String nodeValue) throws XmlException {
        if (document == null) {
            document = createNewDocument();
        }
        Element node = document.createElement(nodeName);
        node.appendChild(document.createTextNode(nodeValue));

        return new XmlNode(this, node);
    }
}

XmlNode.java

package xml.utils;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XmlNode {
    private Element node;
    private XmlDocument parent;

    XmlNode(Element node) {
        this.node = node;
        this.parent = null;
    }

    XmlNode(XmlDocument parent, Element node) {
        this.node = node;
        this.parent = parent;
    }

    Node getNode() {
        return node;
    }

        public String getNodeValue() {
            return node.getTextContent();
        }

    public XmlDocument getParent() {
        return parent;
    }

    public void setParent(XmlDocument parent) {
        this.parent = parent;
    }

    public List<XmlNode> getChildNodes() {
        List<XmlNode> list = new ArrayList<XmlNode>();
        NodeList nodeList = node.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node n = nodeList.item(i);
            if (n.getNodeType() == Node.ELEMENT_NODE) {
                list.add(new XmlNode((Element) n));
            }
        }

        return list;
    }

    public XmlNode getFirstChild() {
        return getChildNodes().get(0);
    }

    public XmlNode getLastChild() {
        List<XmlNode> childs = getChildNodes();
        if (childs.size() == 0)
            return null;

        return childs.get(childs.size() - 1);
    }

    public List<XmlNode> getNodesByTagName(String tagName) {
        List<XmlNode> list = new ArrayList<XmlNode>();
        NodeList nodeList = node.getElementsByTagName(tagName);
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node n = nodeList.item(i);
            if (n.getNodeType() == Node.ELEMENT_NODE) {
                list.add(new XmlNode((Element) n));
            }
        }

        return list;
    }

    public XmlNode getFirstNodeByTagName(String tagName) {
        return getNodesByTagName(tagName).get(0);
    }

    public String getTagValue(String tagName) throws XmlException {
        NodeList tagList = node.getElementsByTagName(tagName);
        if (tagList.getLength() == 0)
            throw new XmlException("Tag: '" + tagName + "' not present");

        NodeList nlList = tagList.item(0).getChildNodes();       
        Node nValue = (Node) nlList.item(0);

        return nValue.getNodeValue();
    }

    public String getAttributeValue(String attributeName) {
        return node.getAttribute(attributeName);
    }

    public String getNodeName() {
        return node.getTagName();
    }

    public void setAttribute(String name, String value) throws XmlException {
        if (parent == null) 
            throw new XmlException("Parent node not present.");

        node.setAttribute(name, value);
    }

    public void setTag(String name, String value) throws XmlException {
        if (parent == null) 
            throw new XmlException("Parent node not present.");

        XmlNode xmlNode = parent.createNode(name, value);
        node.appendChild(xmlNode.node);
    }

    public void addChildNode(XmlNode xmlNode) throws XmlException {
        if (parent == null) 
            throw new XmlException("Parent node not present.");

        node.appendChild(xmlNode.node);
    }

    public XmlNode addChildNode(String nodeName) throws XmlException {
        if (parent == null) 
            throw new XmlException("Parent node not present.");

        XmlNode child = parent.createNode(nodeName);
        node.appendChild(child.getNode());

        return child;
    }
}

Now the DataSet.java and Main.java are as follows:

DataSet.java

package tests;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import xml.utils.XmlDocument;
import xml.utils.XmlException;
import xml.utils.XmlNode;

public class DataSet {
    private int weekNumber;
    private List<Float> employeeRatesLevelA;
    private List<Float> employeeRatesLevelB;

    public DataSet(File xml) throws XmlException {
        employeeRatesLevelA = new ArrayList<Float>();
        employeeRatesLevelB = new ArrayList<Float>();

        loadFromXml(xml);
    }

    private void loadFromXml(File xml) throws XmlException {
        XmlDocument document = new XmlDocument();
        XmlNode root = document.parse(xml);

        weekNumber = Integer.parseInt(root.getTagValue("WeekNumber"));

        XmlNode ratesLevelNode = root.getNodesByTagName("EmployeeRatesLevelA").get(0);
        List<XmlNode> rates = ratesLevelNode.getNodesByTagName("Rate");
        for (XmlNode xmlNode : rates) {
            employeeRatesLevelA.add(Float.parseFloat(xmlNode.getNodeValue()));
        }

        ratesLevelNode = root.getNodesByTagName("EmployeeRatesLevelB").get(0);
        rates = ratesLevelNode.getNodesByTagName("Rate");
        for (XmlNode xmlNode : rates) {
            employeeRatesLevelB.add(Float.parseFloat(xmlNode.getNodeValue()));
        }
    }

    public void display() {
        System.out.println("WeekNumber: " + weekNumber);
        System.out.println("Level A");
        for (Float rate : employeeRatesLevelA) {
            System.out.println("\tRate: " + rate);
        }

        System.out.println("Level B");
        for (Float rate : employeeRatesLevelB) {
            System.out.println("\tRate: " + rate);
        }
    }
}

Main.java

package tests;

import java.io.File;
import java.io.IOException;
import org.xml.sax.SAXException;
import xml.utils.XmlException;

public class Main {
    public static void main(String[] args) throws SAXException, IOException, XmlException {
        File dataFile = new File("/home/jomit/data.xml");
        DataSet dataSet = new DataSet(dataFile);
        dataSet.display();
    }
}
天邊彩虹 2024-12-26 07:35:44

Element.getElementsByTagName("EmployeeRatesLevelA") 其中 Element 应该是 DataSet。或者你可以使用
http://java.sun.com /javase/6/docs/api/org/w3c/dom/Node.html#getChildNodes()

然后过滤所有子节点,直到找到所需的子节点。

Element.getElementsByTagName("EmployeeRatesLevelA") where Element should be DataSet. Or you can use
http://java.sun.com/javase/6/docs/api/org/w3c/dom/Node.html#getChildNodes()

then filter all the childs until you find the ones you want to.

忆梦 2024-12-26 07:35:44

我非常喜欢 groovy 的这些功能,特别是如果将内容加载到数据库中是一次性的:

http://groovy.codehaus.org/Reading+XML+using+Groovy%27s+XmlSlurper

I quite love groovy for these things, particularly if it's a one off to load stuff into a database:

http://groovy.codehaus.org/Reading+XML+using+Groovy%27s+XmlSlurper

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文