简体   繁体   English

解析XML字符串

[英]Parsing XML string

I am a newbie to Java world and struggling to parse a XML string that has attributes in them. 我是Java世界的新手,正在努力解析其中包含属性的XML字符串。

Can anyone help me how to parse this and get all the data? 谁能帮我解析这个问题并获取所有数据? My XML string can change size depending on the NumOfParams. 我的XML字符串可以更改大小,具体取决于NumOfParams。

Below is the XML: 下面是XML:

<PictureManager>
   <FuncName>DisplayImage</FuncName>
   <NumOfParams>2</NumOfParams>
   <Parameters>
      <Param type="integer">10</Param>
      <Param type="String">C://Me.jpg</Param>
   </Parameters>
</PictureManager>

I need to be able to get "integer" and "String" attributes also from the XML. 我还需要能够从XML中获取“整数”和“字符串”属性。 The XML string could grow or shrink based on the < NumOfParams > XML字符串可以根据< NumOfParams >增大或缩小

It will help a lot if you can post some code that actually creates a XML string with the above mentioned tags. 如果您可以发布一些实际上使用上述标记创建XML字符串的代码,则将对您有很大帮助。

Thanks in advance. 提前致谢。

In java libraries exist with methods for parsing XML document. 在Java库中,存在用于解析XML文档的方法。 For example this : 例如:

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    InputSource xml = new InputSource();
    xml.setCharacterStream(xmlSTring);
    Document doc = db.parse(xml);
    NodeList nodes = doc.getElementsByTagName("PictureManager");

    // iterate objects 
    for (int i = 0; i < nodes.getLength(); i++) {
       Element element = (Element) nodes.item(i);
       .
       .
       .
    }

My suggestion is starting from the basic step: 我的建议是从基本步骤开始:

First, you probably need to think about your xml file connection: url? 首先,您可能需要考虑xml文件连接:URL? local? 本地? Second, you need to instance a DocumentBuilderFactory and a builder 其次,您需要实例化一个DocumentBuilderFactory和一个生成器

DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance()。newDocumentBuilder();

OR 要么

URLConnection conn = new URL(url).openConnection(); URLConnection conn =新的URL(url).openConnection(); InputStream inputXml = conn.getInputStream(); InputStream inputXml = conn.getInputStream();

DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance() .newDocumentBuilder(); DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document xmlDoc = docBuilder.parse(inputXml); 文档xmlDoc = docBuilder.parse(inputXml);

The Next step is Parsing XML file: 下一步是解析XML文件:

Document xmlDom = dBuilder.parse(xmlFile); 文档xmlDom = dBuilder.parse(xmlFile);

After that, it turns a xml file into DOM or Tree structure, and you have to travel a node by node to get Attributes or Content . 之后,它将xml文件转换为DOM或Tree结构,并且您必须逐节点移动才能获取AttributesContent In your case, you need to get content. 就您而言,您需要获取内容。 Here is an example: 这是一个例子:

String getContent(Document doc, String tagName){
        String result = "";

        NodeList nList = doc.getElementsByTagName(tagName);
        if(nList.getLength()>0){
            Element eElement = (Element)nList.item(0);
            String ranking = eElement.getTextContent();
            if(!"".equals(ranking)){
                result = String.valueOf(ranking);
            }

        }
        return result;
    }

return of the getContent(xmlDom,"NumOfParams") is "2". getContent(xmlDom,“ NumOfParams”)的返回值为“ 2”。

After you can parse simple xml file, you could try to travel all the xml file and print it out. 解析简单的xml文件之后,您可以尝试遍历所有xml文件并打印出来。 Then get touch with SAX or JAXB. 然后接触SAX或JAXB。

Good luck 祝好运

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

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