在java中动态读取xml元素和值

2022-01-10 00:00:00 xml xml-parsing java

我正在尝试读取 XML(无论如何,我不知道它的结构,bcz 我正在从服务器读取文件.),我在 SO 上进行了很多搜索.但没有发现任何有用的东西.

I'm trying to read XML(whatever, i don't know it's structure, bcz i'm reading flie from server.), and i searched a lot on SO. but not found anything helpful.

1> 我想检索 XML 的节点名称,从根目录到文件末尾、子节点或子节点的子节点,直到没有子节点或兄弟节点.

1> i want to retrieve XML's node name, starting from root to end of file, child or child of child untill there's no child or sibling remaining.

2> 在类似的问题中,我发现他们使用标记名来检索该标记的值,所以我想在其中传递该节点名以获取值...

2> in similar question, i found that they use tagname to retrieve value of that tag, so i want to pass that node name inside this to get value...

是否可以动态进行.或者必须遵循与之前问题相同的结构,例如静态添加标记名以检索值.

is it possible to do dynamically. or have to follow same structure as in previous questions like statically add tagname to retrieve value.

在java中完成后,我想创建可执行jar,并想在欢乐中使用它.

after doing this in java, i want to create executable jar, and want to use it in mirth.

编辑我想找到元素,它的值是这样的,在第一个条件下,我正在循环我的数组列表,我命名为 masterheader,其中包含所有标签名称,在第二个循环中,我将从中查找节点名称xml,并且在这两个 for 循环中,我指定了一个条件,如果两者都匹配,则它获取节点名称并将值存储为分隔形式,因为我想将其转换为 csv.

EDIT i want to find element and it's value such that, in first condition i'm looping my arraylist which i named masterheader which contain all tag name, and in second loop i'm going to find node name from xml, and and inside that two for loop i specified one condition, if both match, then it get node name and it store a value in , separated form, because i want to convert it in csv.

这里,masterheader 和 childheader 是 arraylist.masterheader 默认包含所有标头.我们将在 childheader 中添加节点名,如果两者都匹配,那么我们在结果字符串中添加值.例如masterheader = [员工姓名、员工姓名、父亲姓名、手机、工资]而在childheader = [employeename, mobile,salary]//这是基于xml解析的.

here, masterheader and childheader is arraylist. masterheader contain all header by default. and we gonna add nodename in childheader, nd if both match then we add value in result string. for e.g. masterheader = [employeename, employeesurname, fathername, mobile, salary] and in childheader = [employeename, mobile, salary] //this is based on xml parsing.

for(i=0;i<masterheader.size();i++)
{
    for(j=0;j<childheader.size();j++)
    {
         if((masterheader[i]).equals(childheader[j]))
        {
           //get the value of that node, + ","        
        }
        else
         {
                count++
         }
    }
if(count==childheader.size()){
 //add into result string +null+","  
}
}

推荐答案

您可以为此目的使用 StAX 解析器.

You could use a StAX parser for this purpose.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employees count="4">
    <employee>
        <id>2</id>
        <firstName>Jane</firstName>
        <lastName>Doe</lastName>
        <income>20000.0</income>
    </employee>
    <employee>
        <id>3</id>
        <firstName>Alfred</firstName>
        <lastName>Pennyworth</lastName>
        <income>30000.0</income>
    </employee>
    <employee>
        <id>4</id>
        <firstName>Tony</firstName>
        <lastName>Stark</lastName>
        <income>40000.0</income>
    </employee>
    <employee>
        <id>42</id>
        <firstName>Foo</firstName>
        <lastName>Bar</lastName>
        <income>15.0</income>
    </employee>
</employees>

Java 代码

try (InputStream stream = new FileInputStream("employees.xml")) {
    XMLInputFactory inputFactory = XMLInputFactory.newFactory();
    inputFactory.setProperty(XMLInputFactory.IS_COALESCING, true);

    XMLStreamReader reader = inputFactory.createXMLStreamReader(stream);

    while (reader.hasNext()) {
        switch (reader.next()) {
            case XMLStreamConstants.START_ELEMENT:
                System.out.println("Start " + reader.getName());
                for (int i = 0, count = reader.getAttributeCount(); i < count; i++) {
                    System.out.println(reader.getAttributeName(i) + "=" + reader.getAttributeValue(i));
                }
                break;
            case XMLStreamConstants.END_ELEMENT:
                System.out.println("End " + reader.getName());
                break;
            case XMLStreamConstants.CHARACTERS:
            case XMLStreamConstants.SPACE:
                String text = reader.getText();
                if (!text.trim().isEmpty()) {
                    System.out.println("text: " + text);
                }
                break;
        }
    }
}

请注意,您可以轻松修改 START_ELEMENT 大小写以将标记名称与某个值进行比较.(如果您只想检查这些元素,可能您可以忽略任何其他情况.)

Note that you could easily modify the START_ELEMENT case to compare the tag name to some value. (Probably you could ignore any other cases if you only want to check those elements.)

Start employees
count=4
Start employee
Start id
text: 2
End id
Start firstName
text: Jane
End firstName
Start lastName
text: Doe
End lastName
Start income
text: 20000.0
End income
End employee
Start employee
Start id
text: 3
End id
Start firstName
text: Alfred
End firstName
Start lastName
text: Pennyworth
End lastName
Start income
text: 30000.0
End income
End employee
Start employee
Start id
text: 4
End id
Start firstName
text: Tony
End firstName
Start lastName
text: Stark
End lastName
Start income
text: 40000.0
End income
End employee
Start employee
Start id
text: 42
End id
Start firstName
text: Foo
End firstName
Start lastName
text: Bar
End lastName
Start income
text: 15.0
End income
End employee
End employees

相关文章