1

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

我一直在玩解析文件——尤其是 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
}
}
4

2 回答 2

0

看看 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

于 2013-02-26T15:53:06.680 回答
0

我会沿着 XPath-route 将 XML 文件解析为 Document。

XPath 可用于导航 XML 文档。请参阅 http://www.w3schools.com/xpath/default.asp以获取有关您可以使用 XPaths 实现什么的更多信息。

假设一切都在 main 中完成:

public static void main(String[] args) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(new File("file.xml"));
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    XPathExpression firstnameExpr = xpath.compile("//FirstName");

    NodeList nl = (NodeList) firstnameExpr.evaluate(doc, XPathConstants.NODESET);

    for (int i=0; i<nl.getLength(); i++) {
        Node node = nl.item(i);

        // this is assuming the first child of Firstname is the characters (contents)
        // of the Firstname tag, you may need to do some checking whether or not
        // node.getNodeType() == Node.Text;
        System.out.println("Firstname["+i+"] = " 
                                + node.getChildNodes()[0].getTextContent());
    }


}

除了将名字内容打印到 System.out 之外,您还可以将值添加到 ArrayList 来维护顺序,即:

List<String> firstnameList = new ArrayList<String>();

for (int i=0; i<nl.getLength(); i++) {
    Node node = nl.item(i);

    // again, you might want to check that .getChildNodes() doesn't return null
    // and that it is of type Node.Text
    firstnameList.add(node.getChildNodes()[0].getTextContent());
}
于 2013-06-29T00:51:25.417 回答