簡體   English   中英

Java DOM Parser 讀取 xml 文件信息-節點屬性

[英]Java DOM Parser reading xml files information - nodes attributes

我有一個 xml 文件並嘗試讀取一些信息並嘗試安排它們。 xml 中的數據如下所示:

    <Class code="1-10" kind="category">
        <Meta name="P17b-d" value="2"/>
        <SuperClass code="1-10...1-10"/>
        <SubClass code="1-100"/>
        <Rubric kind="preferred">
            <Label xml:lang="de" xml:space="default">Klinische Untersuchung</Label>
        </Rubric>
    </Class>

我的 Java class 看起來像:

import java.io.File;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;

public class Importer {

   public static void main(String[] args) {

      try {
         File inputFile = new File("ops2022.xml");
         DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
         DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
         Document doc = dBuilder.parse(inputFile);
         doc.getDocumentElement().normalize();
         NodeList nList = doc.getElementsByTagName("Class");
         
         for (int temp = 0; temp < 10; temp++) {
            Node nNode = nList.item(temp);
            System.out.println("\nCurrent Element :" + nNode.getNodeName() );
            Element iElement = (Element) nNode;
            if (nNode.getNodeType() == Node.ELEMENT_NODE && iElement.getAttribute("kind").equals("category")   ) {
               Element eElement = (Element) nNode;
               System.out.println("code : " 
                  + eElement.getAttribute("code"));
               System.out.println("Label : " 
                  + eElement
                  .getElementsByTagName("Label")
                  .item(0)
                  .getTextContent());
               System.out.println("SuperClass : " 
                  + eElement
                  .getElementsByTagName("SuperClass")
                  //I don't know how to get the attribute code here
                  );
            } 
         }
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

但是如何獲取“SuperClass”節點的屬性信息呢? 我不知道為什么 java eElement.getAttributeNode("SuperClass")作為節點處理,盡管它是一個元素。 所以我不能使用 getAttribute()。

我在您的答案 (@Hiran Chaudhuri) 中添加了代碼以獲取我需要的信息:

import java.io.File;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;

public class Importer {

   public static void main(String[] args) {

      try {
         File inputFile = new File("ops2022.xml");
         DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
         DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
         Document doc = dBuilder.parse(inputFile);
         doc.getDocumentElement().normalize();
         NodeList nList = doc.getElementsByTagName("Class");
         
         for (int temp = 0; temp < 10; temp++) {
            Node nNode = nList.item(temp);
            System.out.println("\nCurrent Element :" + nNode.getNodeName() );
            Element iElement = (Element) nNode;
            if (nNode.getNodeType() == Node.ELEMENT_NODE && iElement.getAttribute("kind").equals("category")   ) {
               Element eElement = (Element) nNode;
               System.out.println("code : " 
                  + eElement.getAttribute("code"));
               System.out.println("Label : " 
                  + eElement
                  .getElementsByTagName("Label")
                  .item(0)
                  .getTextContent());
               System.out.println("SuperClass : " 
                  + eElement
                  .getElementsByTagName("SuperClass")
                  Node n = eElement.getElementsByTagName("SuperClass").item(0);
               if (n instanceof Attr) {
                   Attr a = (Attr)n;
                   System.out.println(a.getName());
                   System.out.println(a.getValue());
               } 
                  );
            } 
         }
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

我得到以下信息

----------------------------

Current Element :Class

Current Element :Class

Current Element :Class
code : 1-10
Label : Klinische Untersuchung

如果我添加另一個 else 子句

else {
                   Attr a = (Attr)n;
                   System.out.println(a.getValue());
               }

java 拋出以下錯誤:

java.lang.ClassCastException: class com.sun.org.apache.xerces.internal.dom.DeferredElementImpl cannot be cast to class org.w3c.dom.Attr (com.sun.org.apache.xerces.internal.dom.DeferredElementImpl and org.w3c.dom.Attr are in module java.xml of loader 'bootstrap')
    at Importer.main(Importer.java:46)

.

使用Element.getAttributeNode() ,您確實會收到一個名為 Attr 的 Node 的子類/子接口。 此 Attr 具有您應該感興趣的getName()getValue()方法。

使用Element.getAttribute()將直接傳遞相應屬性的值。

如果你失去了直接獲取正確類型的機會,你仍然可以像這樣恢復

Node n = ... // this is the attribute you are interested in
if (n instanceof Attr) {
    Attr a = (Attr)n;
    System.out.println(a.getName());
    System.out.println(a.getValue());
}

所以您想知道如何訪問 SuperClass 的代碼屬性。 此代碼准確打印一個值:

Document doc = dBuilder.parse(inputFile);
NodeList nList = doc.getElementsByTagName("Class"); // this list only contains Element nodes
for (int temp = 0; temp < nList.getLength(); temp++) {
    Element nNode = (Element)nList.item(temp); // this is one 'class' element

    NodeList nList2 = nNode.getElementsByTagName("SuperClass"); // this list only contains Element nodes
    for (int temp2 = 0; temp2 < nList2.getLength(); temp2++) {
       Element superclass = (Element)nList2.item(temp2);
       String code = superclass.getAttribute("code");
       System.out.println(code);
    }
}

但是這段代碼做同樣的事情:

Document doc = dBuilder.parse(inputFile);
XPath xpath = XPathFactory.newInstance().newXPath();
String code = xpath.evaluate("/Class/SuperClass/@code", doc);

使用 XPath 表達式,您可以更有效地導航 DOM 樹。

以下代碼為我完成了這項工作:

for (int i = 0; i < nList.getLength(); i++) {
            //for (int i = 0; i < 20; i++) {
                Node nNode = nList.item(i);
                
                //System.out.println("\nCurrent Element :" + nNode.getNodeName() );
                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = (Element) nNode;
                    String supString = "OPS-2022";
                    NodeList fieldNodes = eElement.getElementsByTagName("SuperClass");
                    for(int j = 0; j < fieldNodes.getLength(); j++) {
                        Node fieldNode = fieldNodes.item(j);
                        NamedNodeMap attributes = fieldNode.getAttributes();
                        Node attr = attributes.getNamedItem("code");
                        if(attr != null) {
                            supString =attr.getTextContent();
                        }
                    }
                }
            }

謝謝你的幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM