Skip to content

Commit d080454

Browse files
committed
Add processing instruction node support in U.fromXml(string) and U.toXml(map).
1 parent b01e3a7 commit d080454

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/main/java/com/github/underscore/lodash/Xml.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -612,9 +612,13 @@ public static void writeXml(Object value, String name, XmlStringBuilder builder,
612612
+ (addArray ? ARRAY_TRUE : "") + " string=\"true\"/>");
613613
} else {
614614
builder.append("<" + XmlValue.escapeName(name, namespaces)
615-
+ (addArray ? ARRAY_TRUE : "") + ">");
615+
+ (addArray ? ARRAY_TRUE : "") + (name.startsWith("?") ? " " : ">"));
616616
builder.append(escape((String) value));
617-
builder.append("</" + XmlValue.escapeName(name, namespaces) + ">");
617+
if (name.startsWith("?")) {
618+
builder.append("?>");
619+
} else {
620+
builder.append("</" + XmlValue.escapeName(name, namespaces) + ">");
621+
}
618622
}
619623
} else {
620624
processArrays(value, builder, name, parentTextFound, namespaces, addArray);
@@ -709,7 +713,7 @@ public static String escapeName(String name, Set<String> namespaces) {
709713
}
710714
final StringBuilder result = new StringBuilder();
711715
char ch = name.charAt(0);
712-
if (com.sun.org.apache.xerces.internal.util.XMLChar.isNameStart(ch) && ch != ':') {
716+
if (com.sun.org.apache.xerces.internal.util.XMLChar.isNameStart(ch) && ch != ':' || ch == '?') {
713717
result.append(ch);
714718
} else {
715719
result.append("__").append(Base32.encode(Character.toString(ch))).append("__");
@@ -989,7 +993,12 @@ private static Object createMap(final org.w3c.dom.Node node,
989993
final org.w3c.dom.NodeList nodeList = node.getChildNodes();
990994
for (int index = 0; index < nodeList.getLength(); index++) {
991995
final org.w3c.dom.Node currentNode = nodeList.item(index);
992-
final String name = currentNode.getNodeName();
996+
final String name;
997+
if (currentNode.getNodeType() == org.w3c.dom.Node.PROCESSING_INSTRUCTION_NODE) {
998+
name = "?" + currentNode.getNodeName();
999+
} else {
1000+
name = currentNode.getNodeName();
1001+
}
9931002
final Object value;
9941003
if (currentNode.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
9951004
sourceIndex[0] = source.indexOf("<" + name, sourceIndex[0]) + name.length() + 1;

src/test/java/com/github/underscore/lodash/StringTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2127,6 +2127,19 @@ public void toJsonFromXml23() {
21272127
"<?xml version=\"1.0\" encoding=\"UTF-8\"?><root><a></a><a></a></root>")));
21282128
}
21292129

2130+
@SuppressWarnings("unchecked")
2131+
@Test
2132+
public void toJsonFromXml24() {
2133+
final String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<a>\n <?b c=\"d\"?>\n</a>";
2134+
final String json = "{\n"
2135+
+ " \"a\": {\n"
2136+
+ " \"?b\": \"c=\\\"d\\\"\"\n"
2137+
+ " }\n"
2138+
+ "}";
2139+
assertEquals(json, U.toJson((Map<String, Object>) U.fromXml(xml)));
2140+
assertEquals(xml, U.toXml((Map<String, Object>) U.fromJson(json)));
2141+
}
2142+
21302143
@SuppressWarnings("unchecked")
21312144
@Test
21322145
public void toXmlFromJson() {

0 commit comments

Comments
 (0)