简体   繁体   English

如何使用 Parser 从 String(xml) 中获取所有元素<objectnode></objectnode>

[英]How to get all of elements from String(xml) with Parser<ObjectNode>

I get soap xml response and convert to String.我得到 soap xml 响应并转换为字符串。 String (xml) Example:字符串 (xml) 示例:

<Details>
                <row>
                    <item>
                        <name>account</name>
                        <value>45687447</value>
                    </item>
                    <item>
                        <name>number</name>
                        <value>896541</value>
                    </item>
                 </row>

                 <row>
                    <item>
                        <name>account</name>
                        <value>2669874</value>
                    </item>
                    <item>
                        <name>number</name>
                        <value>063641</value>
                    </item>
                 </row>
</Details>

Now i parsing the String like this:现在我像这样解析字符串:

public ObjectNode ParseXml(String xml) {

 Parsing parsing = ParsingFactory.getInstance().create();
        Document document = parsing.xml().document(xml);

    Parser<ObjectNode> parser = parsing.obj("//Details")
            .attribute("row", parsing.obj("//row")
                    .attribute("account", "//item[name/text() = 'account']/value")
                    .attribute("number", "//item[name/text() = 'number']/value")).build();

ObjectNode result = parser.apply(document);


    return result;
}

But problem is the, i take only one row, like this:但问题是,我只拿了一排,像这样:

{
"row": {
        "account": "2669874",
        "number": "063641",
       }
}

If i have 10 rows, but i get only one row.如果我有 10 行,但我只得到一行。 How i can get all rows?我怎样才能得到所有行?

I'm parsing from.xml file which you can easily change and have log on this files too.我正在解析 from.xml 文件,您可以轻松更改该文件并登录该文件。

package java_testiranja;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


/**
 *
 * @author blac
 */
public class Java_testiranja {

    /**
     * @param xml
     * @param args the command line arguments
     */
 
    private List<Employee> myEmpls = new ArrayList<Employee>();

    public static void main(String[] args) throws IOException, ParserConfigurationException {
        Java_testiranja domParser = new Java_testiranja();
        domParser.parseXmlFile();
    }

    private void parseXmlFile() throws IOException, ParserConfigurationException {
        // get the factory
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

        try {

            // Using factory get an instance of document builder
            DocumentBuilder db = dbf.newDocumentBuilder();

            // parse using builder to get DOM representation of the XML file
            Document dom = db.parse("employee.xml");
            parseDocument(dom);
            printData();

        } catch (SAXException se) {
            se.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }

    private void parseDocument(Document dom) {
        // get the root element
        Element docEle = dom.getDocumentElement();

        // get a nodelist of elements
        NodeList nl = docEle.getElementsByTagName("item");
        int stevilor = nl.getLength();
        
        
        if (nl != null && nl.getLength() > 0) {
            
            
            for (int i = 0; i < nl.getLength(); i++) {

                
               
                // get the employee element
                Element el = (Element) nl.item(i);

                // get the Employee object
                Employee e = getEmployee(el);

                // add it to list
                myEmpls.add(e);
            }
        }
    }

    /**
     * I take an employee element and read the values in, create an Employee object and return it
     */
    private Employee getEmployee(Element empEl) {

        // for each <employee> element get text or int values of
        // name ,id, age and name
        
        String name = getTextValue(empEl, "name");
        int id = getIntValue(empEl, "value");
        //int age = getIntValue(empEl, "Age");

        //String type = empEl.getAttribute("type");

        // Create a new Employee with the value read from the xml nodes
        Employee e = new Employee(name, id);

        return e;
    }

    private String getTextValue(Element ele, String tagName) {
        String textVal = null;
        NodeList nl = ele.getElementsByTagName(tagName);
        if (nl != null && nl.getLength() > 0) {
            Element el = (Element) nl.item(0);
            textVal = el.getFirstChild().getNodeValue();
        }

        return textVal;
    }

    private int getIntValue(Element ele, String tagName) {
        // in production application you would catch the exception
        return Integer.parseInt(getTextValue(ele, tagName));
    }

    private void printData() {

     

        Iterator<Employee> it = myEmpls.iterator();
        while (it.hasNext()) {
            System.out.println("{" + "\n" + "\"row\": {");
            System.out.println("        "+it.next().toString());
            System.out.println("        "+it.next().toString());
            System.out.println("       }" + "\n" + "}");
        }
    }
    
}

And the Employee class is like:而员工 class 就像:

package java_testiranja;

/**
 *
 * @author blac
 */
public class Employee {
     private String name;
    private int id;


    public Employee(String name, int id) {
        this.name = name;
        this.id = id;

    }

    public String toString() {
        return "\""+name+"\": " + "\""+id+"\""+",";
    }
    
}

I tried your.xml file and get the result:我尝试了你的 .xml 文件并得到了结果:

{
"row": {
        "account": "45687447",
        "number": "896541",
       }
}
{
"row": {
        "account": "2669874",
        "number": "63641",
       }
}

I just saved.xml file as Employee.xml and called it.我刚刚将 .xml 文件保存为 Employee.xml 并调用了它。 You can simply save this results...您可以简单地保存此结果...

Regards,问候,

Blaz布拉兹

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM