简体   繁体   中英

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. 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. Its writing to a value tag in the XML file that Xpath reports as an element node. 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. 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.

496 Vivid Ave.

Some City, State, 11111

111-222-3333

When the java string is wrote to xml file its entered as: 496 Vivid Ave. Some City, State, 11111 111-222-3333

An example of the node created from the parent program (not my java app) is :

<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:

<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.

The java i'm using looks like:

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. 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.

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行,现在地址块已正确格式化。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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