简体   繁体   English

使用从Java到XML文件的Xpath表达式创建基本的换行符

[英]create basic line breaks with an Xpath Expression from Java to XML file

Throghout learning how to program Stack Overflow has provided a wealth of information, and a staple of learning how to figure out different things. 学习如何编程Stack Overflow的Throghout提供了丰富的信息,并且是学习如何弄清楚不同事物的主要方法。 However for this problem, after searching for a few days and coming up empty handed I've decided to create my first post. 但是对于这个问题,经过几天的搜索并空手而归,我决定创建我的第一篇文章。

The problem, creating a mulit line value in an XML file from a java string using Xpath's Node.setTextContent. 问题是,使用Xpath的Node.setTextContent从Java字符串在XML文件中创建多行值。 Its writing to a value tag in the XML file that Xpath reports as an element node. 它写入Xpath报告为元素节点的XML文件中的value标记。 Now when writing the string value to the node, it does indeed write the string value to the node, however its just one long string value, as the setTextContent method according to the API does not parse any thing in the string and just writes the text. 现在,当将字符串值写入节点时,确实确实将字符串值写入节点,但是它只是一个长字符串值,因为根据API的setTextContent方法不会解析字符串中的任何内容,而只是写入文本。 An example of the data I'm trying to write is:(edited 2-16, just noticed the format option wasn't showing the main post) formated like below with the street, city state and phone broken down like the info below (with out the spaces in between the lines) to match the format as listed in the first the xml code example. 我要写入的数据示例如下:(编辑为2-16,只是注意到format选项未显示主要帖子)的格式如下,街道,城市州和电话均已分解,如下所示(行之间没有空格)以匹配第一个xml代码示例中列出的格式。

496 Vivid Ave. 生动大道496号

Some City, State, 11111 州萨摩市11111

111-222-3333 111-222-3333

When the java string is wrote to xml file its entered as: 496 Vivid Ave. 将Java字符串写入xml文件时,其输入为:496 VividAve。 Some City, State, 11111 111-222-3333 州,一些市,电话:11111 111-222-3333

An example of the node created from the parent program (not my java app) is : 从父程序(不是我的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>

When i run my app the ouput of raw xml data looks like this: 当我运行我的应用程序时,原始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>

I have tried to manually enter the escaped characters for line feed and carriage return in the string , and all it does is prting the code values, not the value. 我尝试手动在字符串中输入换行符和回车符的转义字符,它所做的只是打印代码值而不是值。

As the parent program reads my Java code it does not break down the address and phone number on separate lines. 当父程序读取我的Java代码时,它不会在单独的行中分解地址和电话号码。

The java i'm using looks like: 我正在使用的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;
}

If anyone has any ideas on how to get the Java string to include line breaks that the setTextContent will recognize I would appreaciate it. 如果有人对如何使Java字符串包含换行符有任何想法,则setTextContent将识别出该行,我会予以考虑。 I have tossed the idea around of trying to use text Normalize and white space data, but not really sure how to implement it, and not sure if those methods would be striped out from the setTextContent method anyway. 我已经抛弃了尝试使用文本规范化和空白数据的想法,但是并不确定如何实现它,也不确定这些方法是否会从setTextContent方法中剥离出来。

Thanks for any help! 谢谢你的帮助! I hope I was clear enough in my question to not get flamed! 我希望我的问题足够清楚,不要被解雇!

我在http://xpath.alephzarro.com/content/cheatsheet.html上找到了一个xpather备忘单,其中显示了正则表达式中的特殊字符,而Xpath不会插入换行符的unicoode值,但它确实可以识别\\ n行,现在地址块已正确格式化。

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

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