简体   繁体   English

Java DOM Parser 读取 xml 文件信息-节点属性

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

I have got an xml file and try to read in some information and try to arrange them.我有一个 xml 文件并尝试读取一些信息并尝试安排它们。 The data in the xml looks like: 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>

and my Java class looks like:我的 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();
      }
   }
}

But how do I get the attribute's information of the "SuperClass" Node?但是如何获取“SuperClass”节点的属性信息呢? Idon't know why but java handles eElement.getAttributeNode("SuperClass") as a node, although it is an Element.我不知道为什么 java eElement.getAttributeNode("SuperClass")作为节点处理,尽管它是一个元素。 So I can't use the getAttribute().所以我不能使用 getAttribute()。

I added the code in your answer (@Hiran Chaudhuri) to get my needed information:我在您的答案 (@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();
      }
   }
}

And I get the following我得到以下信息

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

Current Element :Class

Current Element :Class

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

and if I add another else clause like如果我添加另一个 else 子句

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

java throws the following error: 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)

. .

With Element.getAttributeNode() you do receive a subclass/subinterface of Node called Attr.使用Element.getAttributeNode() ,您确实会收到一个名为 Attr 的 Node 的子类/子接口。 This Attr has getName() and getValue() methods that you should be interested in.此 Attr 具有您应该感兴趣的getName()getValue()方法。

Using Element.getAttribute() will directly deliver the value of the corresponding attribute.使用Element.getAttribute()将直接传递相应属性的值。

If you lost the chance to directly obtain the correct type, you can still recover like如果你失去了直接获取正确类型的机会,你仍然可以像这样恢复

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

So you are wondering how to access the SuperClass' code attribute.所以您想知道如何访问 SuperClass 的代码属性。 This code prints exactly the one value:此代码准确打印一个值:

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

However this code does the same:但是这段代码做同样的事情:

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

With XPath expressions you can navigate the DOM tree much more efficiently.使用 XPath 表达式,您可以更有效地导航 DOM 树。

The following code did the job for me:以下代码为我完成了这项工作:

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

Thanks for your help!谢谢你的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM