在JAVA中解析XML,提取特定的数据段
我不知道问我的问题有多准确。我认为有两个问题可能会有所帮助: 我一直在玩解析文件的游戏,尤其是xml 我找到了很多教程和很多技巧 大多数教程都有一个简单的xml文件,首先包含姓名、电话号码等 我的两个问题: 1) 如何仅提取/显示特定数据库之间的数据。例如,如果我只想显示在JAVA中解析XML,提取特定的数据段,java,xml,parsing,Java,Xml,Parsing,我不知道问我的问题有多准确。我认为有两个问题可能会有所帮助: 我一直在玩解析文件的游戏,尤其是xml 我找到了很多教程和很多技巧 大多数教程都有一个简单的xml文件,首先包含姓名、电话号码等 我的两个问题: 1) 如何仅提取/显示特定数据库之间的数据。例如,如果我只想显示,我如何(在Java中)执行以下操作: loop If <tag> = “FirstName” then name_variable = data in between tags); or If <tag
,我如何(在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
循环
如果=“FirstName”,则name\u变量=标记之间的数据);
或
如果=“FirstName”,则为System.out.printf(“名字是%s\n”,name\u变量);
端环
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>
我的公司
史密斯
吉姆
大街123号
我的小镇
总氮
11234
琼斯
约翰
大街456号
高谭
总氮
11234
下面是我拼凑的一些代码,我从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
}
}
在java中打包dom\u解析;
导入org.w3c.dom.*;
导入javax.xml.parsers.*;
导入java.io.*;
导入org.w3c.dom.NodeList;
导入org.w3c.dom.Node;
导入org.w3c.dom.NamedNodeMap;
//导入com.sun.org.apache.xerces.internal.parsers.DOMParser;
JAVA中的公共类DOM_解析_{
公共静态void main(字符串[]args){
//此处的TODO代码应用程序逻辑
String file=“test2.xml”;
如果(args.length>0){
file=args[0];
}//如果结束
试一试{
//DOMParser parser=新的DOMParser();
DocumentBuilderFactory工厂=DocumentBuilderFactory.newInstance();
DocumentBuilder=factory.newDocumentBuilder();
Document=builder.parse(新文件(File));
//Document Document=parser.getDocument();
元素根=document.getDocumentElement();
System.out.println(root.getTagName());
NodeList node_list=root.getElementsByTagName(“执行”);
//节点comp=getNode(“公司”,根);
int i;
对于(i=0;i),请查看StAX API:
(您可能希望使用其“迭代器/事件API”:
下面是一个示例:我将沿着XPath路径将XML文件解析为文档
XPath可用于浏览XML文档。有关XPath的更多信息,请参阅
假设所有工作都已基本完成:
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());
}
}
publicstaticvoidmain(字符串[]args){
DocumentBuilderFactory工厂=DocumentBuilderFactory.newInstance();
DocumentBuilder=factory.newDocumentBuilder();
documentdoc=builder.parse(新文件(“File.xml”);
XPathFactory XPathFactory=XPathFactory.newInstance();
XPath=xPathfactory.newXPath();
XPathExpression firstnameExpr=xpath.compile(“//FirstName”);
NodeList nl=(NodeList)firstnameExpr.evaluate(doc,XPathConstants.NODESET);
for(int i=0;iI昨晚度过(昨晚太晚)试图通过StAX示例和阅读一点内容而感到沮丧。昨晚我遇到了相当程度的挫折。我一直在网络上搜索JAVA解析XML的说明。我认为我的最终目标相当简单——提取一段特定的“标记”并存储在一个变量中XML数据并在其他地方使用。上周,我非常接近使用DOM,但无法将数据存储并传递到另一个部分。我可以显示它,感觉自己就快到了。我可以使用一些额外的建议和指导。我阅读并浏览了所提到的链接。我感觉像在假设某种程度的JA中提到的示例VA熟练程度–示例似乎给出了snip及其部分代码,剩下的代码应该是显而易见的。我可以使用一些额外的建议和指导。谢谢!
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());
}