【问题标题】:how to exclude tag from XML String in java如何在java中从XML字符串中排除标签
【发布时间】:2014-07-26 05:02:01
【问题描述】:

我正在编写一段代码来从网页发送和接收数据。我在java中这样做。但是当我“接收”xml 数据时,它仍然在这样的标签之间

<?xml version='1.0'?>
    <document>
        <title> TEST </title>
    </document>

如何在 Java 中获取没有标签的数据。

这是我尝试过的,该函数写入数据,然后应该得到响应并在System.out.println 中使用它。

public static String User_Select(String username, String password) {

        String mysql_type = "1"; // 1 = Select

        try {
            String urlParameters = "mysql_type=" + mysql_type + "&username=" + username + "&password=" + password;
            URL url = new URL("http://localhost:8080/HTTP_Connection/index.php");
            URLConnection conn = url.openConnection();

            conn.setDoOutput(true);

            OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());

            writer.write(urlParameters);
            writer.flush();

            String line;
            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

            while ((line = reader.readLine()) != null) {
                System.out.println(line);
                //System.out.println("Het werkt!!");
            }
            writer.close();
            reader.close();
            return line;

        } catch (IOException iox) {
            iox.printStackTrace();
            return null;
        }

    }

提前致谢

【问题讨论】:

  • 如果你想做一个获取请求,你应该将 `urlParameters 添加到 URL 并使用 setDoOutput 和 writer 删除部分。
  • 你是说你只想要“TEST”而不是 XML 标签吗?
  • 是的@jedison 这就是我想说的
  • 谢谢,发布了一个可能的答案,应该比 JSoup、DOMParser、XML Parser 等容易得多。
  • @The_Monster 不鼓励 Regex 解析 XML 或 HTML ,请检查此stackoverflow.com/questions/1732348/…

标签: java xml http xml-parsing


【解决方案1】:

我建议简单地使用RegEx 来读取 XML,并获取您所追求的标签内容。 这简化了您需要做的事情,并限制了额外(不必要的)库的包含。 然后有很多关于这个主题的 StackOverflows:Regex for xml parsingIn RegEx, I want to find everything between two XML tags 只是提到其中的两个。

【讨论】:

【解决方案2】:

在 java 中使用 DOMParser。 在 java 文档中进一步检查

【讨论】:

    【解决方案3】:

    使用 XML 解析器来解析您的 XML。这是 Oracle 教程的链接

    Oracle Java XML Parser Tutorial

    【讨论】:

      【解决方案4】:

      只需从URLConnection 传递InputStream

      Document doc = DocumentBuilderFactory.
          newInstance(). 
          newDocumentBuilder().
          parse(conn.getInputStream());
      

      从那里您可以使用 xPath 来查询文档的内容或简单地遍历文档模型。

      查看Java API for XML Processing (JAXP)了解更多详情

      【讨论】:

        【解决方案5】:

        您必须使用 XML 解析器,在您的情况下,完美的选择是 JSoup,它会从网络上抓取数据并解析 XML 和 HTML 格式,它将加载数据并解析它并为您提供所需的内容,这里是一个工作原理的例子:

        1.来自 URL 的 XML

        String xml = Jsoup.connect("http://localhost:8080/HTTP_Connection/index.php")
                    .get().toString();
        Document doc = Jsoup.parse(xml, "", Parser.xmlParser());
        String myTitle=doc.select("title").first();// myTitle contain now  TEST
        

        编辑: 使用以下代码发送 GET 或 POST 参数:

         String xml = Jsoup.connect("http://localhost:8080/HTTP_Connection/index.php")
                        .data("param1Name";"param1Value")
                        .data("param2Name","param2Value").get().toString();
        

        您可以使用get() 调用HTTP GET 方法或post() 调用HTTP POST 方法。

        2。来自字符串的 XML

        您可以使用 JSoup 解析字符串中的 XML 数据:

        String xmlData="<?xml version='1.0'?><document> <title> TEST </title> </document>" ;
        Document doc = Jsoup.parse(xmlData, "", Parser.xmlParser());
        String myTitle=doc.select("title").first();// myTitle contain now  TEST
        

        【讨论】:

        • Mifmif,我应该使用什么进口
        • 下载 JSoup 依赖,将它们包含在您的项目类路径中并导入所需的 JSoup 类。检查我的答案中的 JSoup 链接。
        • 谢谢我去看看
        猜你喜欢
        • 2012-10-11
        • 2019-03-21
        • 2011-04-16
        • 2018-08-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-03
        相关资源
        最近更新 更多