我不确定问我的问题的准确性。我相信两个问题可能会有所帮助:

我一直在分析文件-特别是xml。

我发现了许多教程和多种技巧。

大多数教程都有一个简单的xml文件,以包含名称,电话号码等开头。

我的2个问题:

1)如何提取/显示特定对象之间的数据。例如,如果我只想显示<FirstNames>,我该如何做(在Java中):

loop

If <tag> = “FirstName” then name_variable = data in between tags);

or

If <tag> = “FirstName” then System.out.printf(“ the first name is %s\n”,name_variable);

end loop


2)假设我只在寻找名字的第二个实例,在一些教程/示例中,我已经看到了如何显示循环中的所有数据。我试图将数据设置为等于“数组化”字符串,然后在循环外显示数据,但删除了。最重要的是,如何存储已解析的XML数据的索引(数组)片段以供以后使用或传递给以后的代码?

<company>
<Name>My Company</Name>
<Executive type = "CEO">
    <LastName>Smith</LastName>
    <FirstName>Jim</FirstName>
    <street>123 Main Street</street>
    <city>Mytown</city>
    <state>TN</state>
    <zip>11234</zip>
</Executive>
<Executive type = "OEC">
    <LastName>Jones</LastName>
    <FirstName>John</FirstName>
    <street>456 Main Street</street>
    <city>Gotham</city>
    <state>TN</state>
    <zip>11234</zip>
</Executive>
</company>


这是我拼凑而成的一些代码,我从XML中获取了一些数据,但是还没有弄清楚如何存储在索引的已解析数据中。

package dom_parsing_in_java;
import  org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.NamedNodeMap;
//import com.sun.org.apache.xerces.internal.parsers.DOMParser;

public class DOM_Parsing_In_JAVA {

   public static void main(String[] args) {
    // TODO code application logic here
    String file = "test2.xml";

if(args.length >0){
    file = args[0];

}// end If

try{
    //DOMParser parser= new DOMParser();
    DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(new File(file));

    //Document document = parser.getDocument();

    Element root = document.getDocumentElement();
    System.out.println(root.getTagName());

    NodeList node_list = root.getElementsByTagName("Executive");


   //Node comp = getNode("Company",root);

    int i;


    for(i = 0; i<node_list.getLength();i++){
        Element department = (Element)node_list.item(i);

        System.out.println(department.getTagName());
        System.out.println("name "+document.getElementsByTagName("Name").item(0).getTextContent());
        System.out.println("name "+document.getElementsByTagName("FirstName").item(i).getTextContent());
        System.out.printf(" Lastname: %s%n ", document.getElementsByTagName("LastName").item(i));
        System.out.printf(" Lastname: %s%n ", department.getAttribute("LastName"));
        System.out.printf(" FirstName: %s%n",department.getAttribute("FirstName"));
        //System.out.printf(" elements by Tag %s%n",department.getElementsByTagName("testTag"));
        //System.out.printf(" staff: %s%n",countStaff(department));
    }

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

}//end catch
}
}

最佳答案

看一下StAX API:http://docs.oracle.com/javase/tutorial/jaxp/stax/why.html

(您可能要使用其“迭代器/事件API”:http://docs.oracle.com/javase/tutorial/jaxp/stax/api.html

这是一个示例:http://docs.oracle.com/javase/tutorial/jaxp/stax/example.html#bnbfz

07-26 06:18
查看更多