2014-04-24 45 views
0

的字符串的方法来进行搜索的字符串值XML值以下如何读取使用我在寻找一种方法的Java

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

注意标签不会重复

上述内容存储在字符串中。我想写一个Java方法,它的确是这样的:

tagvalue = getvalue(<TagName>) 

for example tagvalue=getvalye(Name) 

另外,我想这将返回根标签(从字符串1标签)的方法即它应该返回​​。

我知道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 ...任何..如何处理?我知道的一件事是分隔符将是“:”

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

方法调用: 字符串elementvalue = getElementvalue(DataBuffer中, “姓名”)

方法:

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

让我知道如果有人能帮助我吗?

+0

您可以用串读做,递归可能是最好的:读元素的开始,发现元素的结束,并递归地做同样的子标签。 – jedison

+0

你能告诉我们你写的代码来尝试和实现的要求 – user2881767

+2

'我知道Java解析器和DOM可以完成这项任务,但我不想使用它们'为什么你不想使用库你的解析工作? –

回答

0

输入文件

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

方法调用: Tagvalue =的GetValue(DataBuffer中, “名称”);

方法

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

看一看regular expressions

+0

感谢您的回复..我正在寻找一些使用字符串操作的方法。 – user3384231

+0

从我的角度来看,正则表达式是一个没有其他字符串操作。他们经常为仅需要使用String API的几行代码的任务提供智能解决方案。 – Jan

0

你可以用字符串读取,递归可能是最好的:读取元素的开始,找到元素的结束,递归地为儿童标签做同样的事情。如其他地方所指出的,如果标签被重复,这可能会导致问题

+0

这正是我thinking..can请你给一些示例代码来执行此操作...我是新来的Java .. – user3384231

+0

我有点犹豫要做到这一点,因为这往往.equals(“功课分配”)。我们(大多数人在StackOverflow上)倾向于尝试一下,并在遇到困难时寻求帮助。阅读字符串寻找“<”后面跟着“/”,然后阅读“>”。这是你的标签名称;然后用这个标签名称找到匹配的“ jedison

+0

道歉说......它不是homework..but是感谢的伪逻辑...我会尝试再次实现它...感谢您的时间。 – user3384231

0

您也可以使用JAXB来做到这一点。它需要更多的工作,但这将是正确的做法。即使你不想使用它,第二好的东西是Java DOM操作。

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

看看这个article

+0

感谢您的回复。我正在通过字符串操作寻找一些方法,因为我的XML永远不会改变,结构也不会改变。 – user3384231

0

使用indexOf(...)而不是contains(...)并捕获返回的值。然后您可以使用substring(...)获取标签值。

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.

+0

你可以在元素名称中看到我更新的命名空间问题吗? – user3384231

+0

@ user3384231我刚刚更新了我的答案来处理命名空间。 – blazy

+0

非常感谢 – user3384231

相关问题