Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将xml作为字符串传递并读取基于父标记的对象数据_Xml_String - Fatal编程技术网

如何将xml作为字符串传递并读取基于父标记的对象数据

如何将xml作为字符串传递并读取基于父标记的对象数据,xml,string,Xml,String,我将这个字符串传递给一个方法。该方法必须基于标记读取该字符串,并以字符串的形式返回人员数据 String req="<?xml version=\"1.0\"?>" +"<company>" +"<staff id=\"1001\">" +"<firstname>yong</firstname>" +"<lastname>mook kim</lastname>" +"<nickname>mkyong&

我将这个字符串传递给一个方法。该方法必须基于标记读取该字符串,并以字符串的形式返回人员数据

String req="<?xml version=\"1.0\"?>"
+"<company>"
+"<staff id=\"1001\">"
+"<firstname>yong</firstname>"
+"<lastname>mook kim</lastname>"
+"<nickname>mkyong</nickname>"
+"<salary>100000</salary>"
+"</staff>" +"<staff id=\"2001\">"
+"<firstname>low</firstname>"
+"<lastname>yin fong</lastname>"
+"<nickname>fong fong</nickname>"
+"<salary>200000</salary>"
+"</staff>" +"</company>";
String req=“”
+""
+""
+“勇”
+“mook kim”
+“mkyong”
+"100000"
+"" +""
+“低”
+“贤芳”
+“芳芳”
+"200000"
+"" +"";
使用JDOM:

String xml = "<message>HELLO!</message>";
org.jdom.input.SAXBuilder saxBuilder = new SAXBuilder();
try {
    org.jdom.Document doc = saxBuilder.build(new StringReader(xml));
    String message = doc.getRootElement().getText();
    System.out.println(message);
} catch (JDOMException e) {
    // handle JDOMException
} catch (IOException e) {
    // handle IOException
}
stringxml=“你好!”;
org.jdom.input.SAXBuilder SAXBuilder=new SAXBuilder();
试一试{
org.jdom.Document doc=saxBuilder.build(新的StringReader(xml));
字符串消息=doc.getRootElement().getText();
System.out.println(消息);
}捕获(JDOMException e){
//句柄JDOMException
}捕获(IOE异常){
//处理IOException
}
使用Xerces DOMParser:

String xml = "<message>HELLO!</message>";
DOMParser parser = new DOMParser();
try {
    parser.parse(new InputSource(new java.io.StringReader(xml)));
    Document doc = parser.getDocument();
    String message = doc.getDocumentElement().getTextContent();
    System.out.println(message);
} catch (SAXException e) {
    // handle SAXException 
} catch (IOException e) {
    // handle IOException 
}
stringxml=“你好!”;
DOMParser parser=新的DOMParser();
试一试{
parser.parse(新的InputSource(新的java.io.StringReader(xml));
Document doc=parser.getDocument();
字符串消息=doc.getDocumentElement().getTextContent();
System.out.println(消息);
}捕获(SAXE异常){
//处理SAXException
}捕获(IOE异常){
//处理IOException
}
使用JAXP接口:

String xml = "<message>HELLO!</message>";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
try {
    db = dbf.newDocumentBuilder();
    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(xml));
    try {
        Document doc = db.parse(is);
        String message = doc.getDocumentElement().getTextContent();
        System.out.println(message);
    } catch (SAXException e) {
        // handle SAXException
    } catch (IOException e) {
        // handle IOException
    }
} catch (ParserConfigurationException e1) {
    // handle ParserConfigurationException
}
stringxml=“你好!”;
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=null;
试一试{
db=dbf.newDocumentBuilder();
InputSource is=新的InputSource();
is.setCharacterStream(新的StringReader(xml));
试一试{
文档doc=db.parse(is);
字符串消息=doc.getDocumentElement().getTextContent();
System.out.println(消息);
}捕获(SAXE异常){
//处理SAXException
}捕获(IOE异常){
//处理IOException
}
}捕获(ParserConfiguration异常e1){
//句柄ParserConfiguration异常
}

hI Joy,您能发布一个示例输入并输出您的期望值吗?这将非常有用。输入仅在字符串上方。输出为1001 yong mook kim mkyong 10000 2001 low yin fong fong fong 200000我可以根据父标记获取数据吗。指每个员工的信息。