如何在Java中解析XML根元素并选择它的一些值?

如何在Java中解析XML根元素并选择它的一些值?,java,xml,jdom,Java,Xml,Jdom,我正在寻找一种解析xml根元素并从中获取一些值的实用方法。 我试过很多方法,但没有一种是有效的 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); Document doc = factory.newDocumentBuilder().parse(fileLoaded); Element root = null;

我正在寻找一种解析xml根元素并从中获取一些值的实用方法。 我试过很多方法,但没有一种是有效的

       DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

                Document doc = factory.newDocumentBuilder().parse(fileLoaded);

                Element root = null;

                NodeList list = doc.getChildNodes();
               System.out.println(list.toString());
                for (int i = 0; i < list.getLength(); i++) {
                  if (list.item(i) instanceof Element) {
                    root = (Element) list.item(i);
                    System.out.println(root.toString());
                    break;
                  }
                }
                root = doc.getDocumentElement();
              }
DocumentBuilderFactory=DocumentBuilderFactory.newInstance();
Document doc=factory.newDocumentBuilder().parse(文件加载);
元素根=空;
NodeList list=doc.getChildNodes();
System.out.println(list.toString());
对于(int i=0;i
XML文件:

    <planes_for_sale id="planeId" num="1452" name="boing">
    <ad>
      <year> 1977 </year>
      <make> test </make>
      <model> Skyhawk </model>
      <color> Light blue and white </color>
      <description> New paint, nearly new interior,
        685 hours SMOH, full IFR King avionics </description>
      <price> 23,495 </price>
      <seller phone = "555-222-3333"> Skyway Aircraft </seller>
      <location>
      <city> Rapid City, </city>
      <state> South Dakota </state>
  </location>

1977
试验
天鹰
浅蓝色和白色
新油漆,新内饰,
685小时烟雾,全IFR国王航空电子设备
23,495 
空中飞机
快速城市,
南达科他州


在我的例子中,我想从
planes\u for\u sale
根元素加载
id
num
name

尝试封送和解封机制

JAXBContext jaxbContext = JAXBContext.newInstance(EmployeeMap.class);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    EmployeeMap empMap = (EmployeeMap) jaxbUnmarshaller.unmarshal( new File("c:/temp/employees.xml") );

对于任何正在寻找实用方法的人来说,这里有一个测试:

    DocumentBuilderFactory factory = DocumentBuilderFactory
                    .newInstance();

            Document doc = factory.newDocumentBuilder().parse(fileLoaded);
            doc.getDocumentElement().normalize();
            System.out.println("Root element :"
                    + doc.getDocumentElement().getNodeName());
            NodeList nList = doc.getElementsByTagName("planes_for_sale");

              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;
                        System.out.println("Plane name : " 
                           + eElement.getAttribute("name"));
                    }
                 }
DocumentBuilderFactory=DocumentBuilderFactory
.newInstance();
Document doc=factory.newDocumentBuilder().parse(文件加载);
doc.getDocumentElement().normalize();
System.out.println(“根元素:”
+doc.getDocumentElement().getNodeName());
NodeList nList=doc.getElementsByTagName(“出售的飞机”);
System.out.println(“-------------------------------”;
对于(int-temp=0;temp
使用XPath提取属性值和元素内容

    DocumentBuilder builder = DocumentBuilderFactory
                                 .newInstance()
                                 .newDocumentBuilder();

    Document doc = builder.parse(...);

    XPath xp = XPathFactory
                  .newInstance()
                  .newXPath();

    String id = xp.evaluate("planes_for_sale/@id", doc);
    String num = xp.evaluate("planes_for_sale/@num", doc);
    String name = xp.evaluate("planes_for_sale/@name", doc);

    System.out.println("id: " + id);
    System.out.println("num: " + num);
    System.out.println("name: " + name);
产生:

id: planeId
num: 1452
name: boing
看看这个答案

它向你展示了如何

  • 将XML文件读入DOM
  • 使用XPath筛选出一组节点
  • 对每个提取的节点执行特定操作
这里可以找到支持XPath2.0的变体


thnx以获取您的答案,但在我的例子中,如果您看一下我的xml示例,它的xml结构就不一样了,因此我认为这样做是不对的。