0

Throghout 学习如何编程 Stack Overflow 提供了丰富的信息,并且是学习如何找出不同事物的主要内容。但是对于这个问题,在搜索了几天并空手而归之后,我决定创建我的第一篇文章。

问题是,使用 Xpath 的 Node.setTextContent 从 Java 字符串在 XML 文件中创建多行值。它写入 Xpath 报告为元素节点的 XML 文件中的值标记。现在在将字符串值写入节点时,它确实将字符串值写入节点,但是它只是一个长字符串值,因为根据 API 的 setTextContent 方法不会解析字符串中的任何内容,而只是写入文本. 我正在尝试编写的数据示例是:(已编辑 2-16,只是注意到格式选项未显示主要帖子)格式如下所示,街道、城市状态和电话分解如下(行之间没有空格)以匹配第一个 xml 代码示例中列出的格式。

生动的大街 496 号。

一些城市,州,11111

111-222-3333

当将 java 字符串写入 xml 文件时,其输入为:496 Vivid Ave. Some City, State, 11111 111-222-3333

从父程序(不是我的 java 应用程序)创建的节点的示例是:

<field sid="ADDRESS">
     <itemlocation>
        <ae>
           <ae>absolute</ae>
           <ae>33</ae>
           <ae>168</ae>
        </ae>
     </itemlocation>
     <value>496 Vivid Ave.
Some City, State, 11111
111-222-3333</value>
     <borderwidth>0</borderwidth>
     <fontinfo>
        <ae>Times New Roman</ae>
        <ae>10</ae>
        <ae>plain</ae>
     </fontinfo>
     <scrollhoriz>wordwrap</scrollhoriz>
     <scrollvert>fixed</scrollvert>
     <format>
        <ae>string</ae>
        <ae>optional</ae>
     </format>
     <acclabel>6.  leave address.
enter street, city, state, zip code and phone number.
</acclabel>
     <size>
        <ae>35</ae>
        <ae>3</ae>
     </size>
     <previous>FIELD5</previous>
     <next>ORDINARY</next>
  </field>

当我运行我的应用程序时,原始 xml 数据的输出如下所示:

<field sid="ADDRESS">
     <itemlocation>
        <ae>
           <ae>absolute</ae>
           <ae>33</ae>
           <ae>168</ae>
        </ae>
     </itemlocation>
     <value>496 Vivid Ave. Some City, State, 11111 111-222-3333</value>
     <borderwidth>0</borderwidth>
     <fontinfo>
        <ae>Times New Roman</ae>
        <ae>10</ae>
        <ae>plain</ae>
     </fontinfo>
     <scrollhoriz>wordwrap</scrollhoriz>
     <scrollvert>fixed</scrollvert>
     <format>
        <ae>string</ae>
        <ae>optional</ae>
     </format>
     <acclabel>6.  leave address.
enter street, city, state, zip code and phone number.
</acclabel>
     <size>
        <ae>35</ae>
        <ae>3</ae>
     </size>
     <previous>FIELD5</previous>
     <next>ORDINARY</next>
  </field>

我试图在字符串中手动输入换行符和回车符的转义字符,它所做的只是打印代码值,而不是值。

当父程序读取我的 Java 代码时,它不会将地址和电话号码分成不同的行。

我正在使用的 java 看起来像:

public void setFieldValue(String sid, String value){
    Node resultNode = null;
    XPath xPath = XPathFactory.newInstance().newXPath();

    try{

        resultNode = (Node)xPath.evaluate(makeFieldString(sid), doc,XPathConstants.NODE);

    }catch(XPathExpressionException xpee){System.out.println(xpee + "Could not retrive node");}
     if (resultNode != null){

        resultNode.setTextContent(value);

    }
    else System.out.println("Epic fail");

    }

public String makeFieldString(String sid){
    String output=null;
    if (sid.equals("POPUP2")){
        output="//popup[@sid='" + sid + "']/value";
    }

    else{
       output=  "//field[@sid='" + sid + "']/value";
    }
    return output;
}

如果有人对如何让 Java 字符串包含 setTextContent 将识别的换行符有任何想法,我会欣赏它。我已经放弃了尝试使用文本标准化和空白数据的想法,但不确定如何实现它,也不确定这些方法是否会从 setTextContent 方法中删除。

谢谢你的帮助!我希望我的问题足够清楚,不会被激怒!

4

1 回答 1

0

我在http://xpath.alephzarro.com/content/cheatsheet.html找到了一个 xpather cheetsheet,它显示了正则表达式中的特殊字符,而 Xpath 不会插入换行符的 unicocode 值,它确实识别 \n 为 new行,现在地址块的格式正确。

于 2013-02-16T16:09:45.010 回答