如何在XML中从一个字段转到另一个字段

时间:2015-02-04 06:25:26

标签: java xml-parsing

<table name="categoryConfigTable">
   <row>
      <field name="mediaConfigId">0</field>
      <field name="startDate">2005-01-01</field>
      <field name="endDate">2025-12-31</field>
      <field name="class">all</field>
      <field name="CID">10</field>
      <field name="sequenceNum">1</field>
      <field name="parentCID">NULL</field>
   </row>
</table>

这是我的XML文件的一部分,我想检索parentCID为NULL的CID值

我的JAVA代码的一部分是 ` public static void main(String [] args){

    try {
         File fXmlFile = new File("/home/media.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);    
        doc.getDocumentElement().normalize();
                System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
        NodeList nlist = doc.getElementsByTagName("field");
        int len = nlist.getLength();
        for (int i = 0; i < len; i++) {
            Node node = nlist.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                Element e = (Element)node;
                String attrVal = e.getAttribute("name"); 
                try {

                   if(attrVal.equalsIgnoreCase("parentCID") && e.getTextContent().equals("NULL"))
                        {
                             System.out.println("root element" +e.getTextContent());
                        }
                }
                catch (IOException ie) {
                    //exception handling left as an exercise for the reader
                }
            }
    }}catch (Exception e) {
        e.printStackTrace();
        }
}`

从这里我得到所有parentCID的值为null但是我想去CID如何做到这一点?

1 个答案:

答案 0 :(得分:0)

你可以像这样得到它

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

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

public class ReadXMLFile {

    public static void main(String argv[]) {

        try {

            File fXmlFile = new File("src/test.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(fXmlFile);

            doc.getDocumentElement().normalize();

            System.out.println("Root element :"
                    + doc.getDocumentElement().getNodeName());

            NodeList nList = doc.getElementsByTagName("row");

            System.out.println("----------------------------");

            for (int temp = 0; temp < nList.getLength(); temp++) {

                Node nNode = nList.item(temp);

                //System.out.println("\nCurrent Element :" + nNode.getNodeName());

                if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                    Element eElement = (Element) nNode;

                    NodeList nList2 = eElement.getElementsByTagName("field");

                    String cidvalue=null;

                    for (int temp1 = 0; temp1 < nList2.getLength(); temp1++) {

                        Node nNode1 = nList2.item(temp1);

                        //System.out.println("\nCurrent Element Internal :"+ nNode1.getNodeName());

                        if (nNode1.getNodeType() == Node.ELEMENT_NODE) {
                            Element eElement1 = (Element) nNode1;



                            if(eElement1.getAttribute("name").equalsIgnoreCase("CID"))
                             {


                                cidvalue=eElement1.getTextContent();
                             }

                             if(eElement1.getAttribute("name").equalsIgnoreCase("parentCID") && (eElement1.getTextContent().equalsIgnoreCase("NULL")))
                             {
                                // System.out.println(eElement1.getTextContent());

                                 System.out.println("row["+temp+"] where parentCID is NULL and corresponding CID value :: "+cidvalue);
                                 cidvalue=null;

                             }


                        }
                    }

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}