如何使用Java的字符串方法读取XML值

时间:2014-04-24 15:35:52

标签: java xml string

我正在寻找一种从以下

中搜索字符串值的方法
<Test>
<Name>XYZ</Name>
<City>London</City>
</Test>

请注意,标签不会重复

以上内容存储在String中。我想编写一个类似这样的java方法:

tagvalue = getvalue(<TagName>)

for example tagvalue=getvalye(Name)

另外,我想要一个返回根标签的方法(字符串中的第一个标签),即它应该返回"Test"

我知道Java解析器和DOM可以执行此任务,但我不想使用它们。我可以通过字符串操作方法简单地完成吗?还是其他一些方法?

我正在尝试的解决方案如下:

//Method Call
String tag_value = getvalue(databuffer,"Name");

//Method

    private String getvalue(String Content, String Tagname) 
     {

            if (Content.contains("<"+Tagname+">"))
                {
                      if(Content.contains("</"+Tagname+">"))
                        {
                          //How do I get Tag value here? 
                 return Tagvalue;
                        }
                    }
        }

我尝试的另一个解决方案是:

//method 
    private String getvalue(String Content, String Tagname)
        {
                //<Name>XYZ</Name> 
            int Starttag = Content.indexOf("<"+Tagname+">");//Returns 0
            int Endtag   = Content.indexOf("</"+Tagname+">");//Returns 9 
                //How to find the total length of <Starttag> so that I can subtract it with the index of Endtag to get the value?
            return ;
        }

我能得到任何人的帮助吗?

// 我已在元素名称中更新了我的名称空间问题...

输入文件如下所示

<ns:Test>
<ns:Name>XYZ</ns:Name>
<ns:City>London</ns:City>
</ns:Test>

注意:我不知道将来的命名空间?它可能是ns或np ...... what..how如何处理?我知道的一件事是分隔符将是&#34;:#34;

是否可以使用字符串方法简单地忽略元素名称中的名称空间以获取以下逻辑中的元素值?

方法调用: String elementvalue = getElementvalue(databuffer,&#34; Name&#34;)

方法:

public String getvalue(String buffer, String tagname) 
    {   
        String startTag, endTag,elementdata = null;
        int startposition,endposition;
        try 
        {
            startTag     = "<"+ tagname + ">";
            endTag       = "</"+ tagname + ">";
            startposition         = buffer.indexOf(startTag);       
            startposition         = startposition + startTag.length();
            endposition      = buffer.indexOf(endTag); 
            elementdata =  buffer.substring(startposition, endposition);
        }
        return elementdata;
    }

让我知道是否有人可以帮助我?

6 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

你可以用字符串读取来做,递归可能是最好的:读取元素的开始,找到元素的结束,并递归地为子标记执行相同的操作。如其他地方所述,如果重复标记,这可能会导致问题。

答案 2 :(得分:0)

您也可以使用JAXB来执行此操作。它需要更多的工作,但这是正确的方法。接下来最好的事情是Java DOM操作,即使你不想使用它。

使用正则表达式或字符串操作很危险,因为您的XML可能会发生变化。

看看这个article

答案 3 :(得分:0)

使用indexOf(...)代替contains(...)并捕获返回的值。然后,您可以使用substring(...)获取标记值。

答案 4 :(得分:0)

你的第二个解决方案更优雅。我修改了您的代码并添加了一些注释,以便您可以更好地了解如何进行操作。

private static String getvalue(String content, String tagname)
{
    // <ns:Name>XYZ</ns:Name> 
    int tagOpenIndex = content.indexOf(tagname + ">"); //returns the start index of the opening tag or -1 if it was not found
    int tagCloseIndex   = content.indexOf("</", tagOpenIndex); //returns the start index of the closing tag or -1 if it was not found
    String res = null;
   // How to find the total length of <Starttag> so that I can subtract it with the index of Endtag to get the value?
    if(tagOpenIndex != -1 && tagCloseIndex != -1 && content.indexOf(":" + tagname, tagCloseIndex) != -1){ // Firstly, you should be sure the opening and closing tags were both found in the String
         // The length of the opening tag = the length of the tag itself + 1 (the closing delimiter ">")
        int lenOpenTag = tagname.length() + 1;
        // the start index of the value enclosed in the tags is the start index of the opening tag + the length of the opening tag 
        int valueStartIndex = tagOpenIndex + lenOpenTag; 
        // you simply get the substring between the valueStartIndex and the start index of the closing tag
        res = content.substring(valueStartIndex, tagCloseIndex);
    }
    return res;
}

建议: try and cultivate the habit of starting your variable names with small letters except for constants.

答案 5 :(得分:0)

输入文件

<Test>
<Name>XYZ</Name>
<City>London</City>
</Test>

方法调用: Tagvalue = getvalue(databuffer,&#34; Name&#34;);

方法

public String getvalue(String buffer, String tagname) 
    {   
        String startTag, endTag,elementdata = null;
        int startposition,endposition;
        try 
        {
            startTag     = "<"+ tagname + ">";
            endTag       = "</"+ tagname + ">";
            startposition         = buffer.indexOf(startTag);       
            startposition         = startposition + startTag.length();
            endposition      = buffer.indexOf(endTag); 
            elementdata =  buffer.substring(startposition, endposition);
        }
        return elementdata;
    }
相关问题