簡體   English   中英

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

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

我得到 soap xml 響應並轉換為字符串。 字符串 (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>

現在我像這樣解析字符串:

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;
}

但問題是,我只拿了一排,像這樣:

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

如果我有 10 行,但我只得到一行。 我怎樣才能得到所有行?

我正在解析 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" + "}");
        }
    }
    
}

而員工 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+"\""+",";
    }
    
}

我嘗試了你的 .xml 文件並得到了結果:

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

我剛剛將 .xml 文件保存為 Employee.xml 並調用了它。 您可以簡單地保存此結果...

問候,

布拉茲

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM