Java xml解析具有相同名称的两个标记

时间:2015-07-07 17:05:06

标签: java xml parsing dom tags

我正在编写一个解析xml文件的代码。我想从中检索国家标记值并将其保存到String []。当我想要做的时候,它只检索一个值。但我在xml中重复了两次国家标记。将来它可能是三次或四次,依此类推,这是动态的。这是我的xml:

<?xml version="1.0"?>
<metadata>
<Country>All Countries</Country>
<Country>US - United States</Country>
</metadata>

这是我的代码:

private static String parseXml(String xmlData)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilder builder = DocumentBuilderFactory.newInstance()
            .newDocumentBuilder();
    InputSource src = new InputSource();
    src.setCharacterStream(new StringReader(xmlData));
    Document doc = builder.parse(src);
    NodeList nodes = doc.getElementsByTagName("metadata");
    Element line = null;
    String cnt = null;
    for (int i = 0; i < nodes.getLength(); i++) {
        Element element = (Element) nodes.item(i);
        NodeList country = element.getElementsByTagName("Country");
        for (int j = 0; j < country.getLength(); j++) {
            if (country != null) {
                System.out.println("Country not null " + country);
                line = (Element) country.item(0);
                if (line != null) {
                    cnt = getCharacterDataFromElement(line);
                }
            }
        }
    }
    return cnt;

}

public static String getCharacterDataFromElement(Element e) {
    Node child = e.getFirstChild();
    if (child instanceof CharacterData) {
        CharacterData cd = (CharacterData) child;
        if (cd != null) {
            return cd.getData();
        }
    }
    return "";
}

4 个答案:

答案 0 :(得分:2)

您的代码不提供在代码中存储国家/地区列表的任何可能性,尽管它会遍历所有国家/地区。

以下内容应该有所帮助(虽然未经测试): 将String cnt = null;更改为List<String> = new ArrayList<String>(),而不是

for (int j = 0; j < country.getLength(); j++) {
    if (country != null) {
        System.out.println("Country not null " + country);
        line = (Element) country.item(0);
        if (line != null) {
            cnt = getCharacterDataFromElement(line);
        }
    }
}

以下内容:

if (country != null) {
    System.out.println("Country not null " + country);
    for (int j = 0; j < country.getLength(); j++) {
        line = (Element) country.item(j);
        if (line != null) {
            cnt.add( getCharacterDataFromElement(line));
        }
    }
}

答案 1 :(得分:1)

您需要在程序中进行两次修改

  1. item(0)应替换为item(j)

  2. 你可以得到的价值 国家/地区使用line.getFirstChild().getNodeValue()

  3. 以下是更新的课程XmlParser.java

    import java.io.IOException;
    import java.io.StringReader;
    
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    import org.xml.sax.InputSource;
    import org.xml.sax.SAXException;
    
    public class XmlParser {
    
    
        public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
            String xml = "<?xml version=\"1.0\"?> "
                    + "<metadata> "
                    + "<Country>All Countries</Country> "
                    + "<Country>US - United States</Country> "
                    + "</metadata>";
            parseXml(xml);
        }
    
        private static String parseXml(String xmlData)
                throws ParserConfigurationException, SAXException, IOException {
            DocumentBuilder builder = DocumentBuilderFactory.newInstance()
                    .newDocumentBuilder();
            InputSource src = new InputSource();
            src.setCharacterStream(new StringReader(xmlData));
            Document doc = builder.parse(src);
            NodeList nodes = doc.getElementsByTagName("metadata");
            Element line = null;
            String cnt = null;
            for (int i = 0; i < nodes.getLength(); i++) {
                Element element = (Element) nodes.item(i);
                NodeList country = element.getElementsByTagName("Country");
                for (int j = 0; j < country.getLength(); j++) {
                    if (country != null) {
                        System.out.println("Country not null " + country);
                        line = (Element) country.item(j);
                        System.out.println("value :: " + line.getFirstChild().getNodeValue());
                    }
                }
            }
            return cnt;
    
        }
    
        public static String getCharacterDataFromElement(Element e) {
            Node child = e.getFirstChild();
            if (child instanceof org.w3c.dom.CharacterData) {
                org.w3c.dom.CharacterData cd = (org.w3c.dom.CharacterData) child;
                if (cd != null) {
                    return cd.getData();
                }
            }
            return "";
        }
    }
    

    以下output

      

    国家/地区不为空   com.sun.org.apache.xerces.internal.dom.DeepNodeListImpl@51f45f2a value   ::所有国家

         

    Country not null com.sun.org.apache.xerces.internal.dom.DeepNodeListImpl@51f45f2a value   ::美国 - 美国

答案 2 :(得分:1)

试试这个:

public class Test {

    public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException {
        System.out
            .println(Arrays
                    .toString(parseXml("<?xml version=\"1.0\"?><metadata><Country>All Countries</Country><Country>US - United States</Country></metadata>")));
    }

    private static String[] parseXml(String xmlData) throws SAXException, IOException, ParserConfigurationException {
        ArrayList<String> list = new ArrayList<String>();
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        InputSource src = new InputSource();
        src.setCharacterStream(new StringReader(xmlData));
        Document doc = builder.parse(src);
        NodeList nodes = doc.getElementsByTagName("metadata");
        for (int i = 0; i < nodes.getLength(); i++) {
            Element element = (Element) nodes.item(i);
            NodeList country = element.getElementsByTagName("Country");
            for (int j = 0; j < country.getLength(); j++) {
                list.add(country.item(j).getTextContent());
            }
        }
        return list.toArray(new String[list.size()]);
    }
}

答案 3 :(得分:1)

private static String[] getCountries(String xmlData) throws Exception{
    List<String> result = new ArrayList<String>();
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    InputSource src = new InputSource(new StringReader(xmlData));
    Document doc = builder.parse(src);
    NodeList nodes = doc.getElementsByTagName("metadata");
    for(int i = 0; i < nodes.getLength(); i++){
        Element metadata = (Element)nodes.item(i);
        NodeList countries = metadata.getElementsByTagName("Country");
        for(int j = 0; j < countries.getLength(); j++){
            result.add(countries.item(j).getTextContent());
        }
    }
    return result.toArray(new String[result.size()]);
}
相关问题